package Server.Poker.LABJZ;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Vector;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import com.mysql.cj.mysqlx.protobuf.MysqlxDatatypes.Array;

import Server.Agent.GameDB;
import Server.Model.Base.IRoom;
import Server.Model.Base.IServerUserItem;
import Server.Model.Base.SHMUser;
import Server.Model.Base.SecShmUser;
import Server.Model.Base.UserGoldChange;
import Server.Model.Base.UserRmbChange;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import club.ClubRedis;
import common.routine.JsonUtils;
import redis.protocal.DbInfoMgr;
import redis.protocal.DwcRedisRecord;
import redis.protocal.UserRedisInfo;
import robot.GoldMgr;
import Server.Poker.Replay.*;
import admin.AdminRedis;
// 比鸡每一局的战绩
class BJRoundRecord {
		// 战绩记录
		public int m_vecRecordRoundPai[][] = new int[5][9]; // 每一局牌值
		public int m_vecRecordRoundScore[] = new int[5];// 每一局的总分数
		public int m_vecRecordRoundScoreDetail[][] = new int[5][3]; // 每一局每道牌的得分
		public String m_vecRecordRoundXifen[] = new String[5]; // 每一局每道牌的喜分
		public int m_vecPlayerChairId[] = new int [5];
		public String m_names[] = new String[5];
		
		public void initData()
		{
			for(int i = 0; i < 5; i ++)
			{
				m_vecPlayerChairId[i] = -1;
			}
		}
};

class BJZTable extends IRoom {

	public static final int US_NULL = 0;
	public static final int US_READY = 1;
	public static final int US_PLAY = 2;

	public Replay Replay = new Replay();
	
	@Override
	public int GetID() {
		return m_dwTableID;
	};

	@Override
	public int GetCreator() {
		return m_Creator;
	}

	@Override
	public String GetType() {
		return "LABJ";
	};

	@Override
	public void OnClientClose(int uid) {
		OnUserStandUp(uid,false);
	};

	public boolean SendData(int wChairID, String sData) {
		// 群发用户
		if (wChairID == Constants.INVALID_CHAIR) {
			for (short i = 0; i < m_pIUserItem.length; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					if (m_pIUserItem[i].isAndroidUser == 0) {
						dthall.getInstance().onSend(m_pIUserItem[i].dwUserID, sData);
					} else {
						// mPlayers[i].SendGameMessage(wSubCmdID, pData,
						// wDataSize, data);
					}
				}
			}

			return true;
		} else {
			if (wChairID >= m_pIUserItem.length) {
				dthall.getInstance().onSend(wChairID, sData);
			} else if (m_pIUserItem[wChairID].dwUserID > 0) {
				if (m_pIUserItem[wChairID].isAndroidUser == 0) {
					dthall.getInstance().onSend(m_pIUserItem[wChairID].dwUserID, sData);
				} else {
				}
				return true;
			}
		}

		return false;
	}

	int m_dwLastDoCmdTime;

	@Override
	public int CanSit(int _Uid) {
		if(this.isDwcType()){
			UserRedisInfo info = dthall.getInstance().getSafeUserRedisInfo(_Uid);
			if(info.json.optInt("userRmbType") == 1 && !canUserJoinRentRoom(info.json.optInt("upAgentUid"))){
				return 22;
			}else if(info.json.optInt("userRmbType") == 0 && m_clubOwnerId != 0){
				UserRedisInfo clubInfo = dthall.getInstance().getSafeUserRedisInfo(m_clubOwnerId);
				if(clubInfo.json.optInt("userRmbType") == 1){
					return 23;
				}
			}
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == _Uid) {
				return i;
			}
		}
		
		if (m_locked != 0) {
			return 13;// 已经开始 并且房主不让近
		}
		
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		
		if (cntUser >= m_byMaxUser) {
			return 13;
		}
		
		if (user != null) {
			int check = m_byCostGem;
			if (user.getGem(0) < check) {
				if(m_payWay != "FZPAY"){
					return 11;
				}
			}
		}
		if(user != null && isGoldType()){
			if(user.getCoin(0) < m_ruchang){
				return 21;
			}
		}
		if(user != null && isDwcType()){
			if(user.getCoin(0) < m_ruchang){
				return 21;
			}
		}
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				return i;
			}
		}
		
		return 12;
		
	}


	Long m_lStartTime;
	long needDealTime;
	int m_dwTableID;
	long m_dismissTimer;

	int m_byAgreeDismiss[] = new int[5];

	boolean m_bGameStarted;
	boolean m_bTableStarted;
	boolean m_bPeiPaiReady; // 是否开始配牌
	boolean m_bLookOnDownTimeStart; // 是否开启器旁观者离开房间倒计时

	int m_byRound;
	int m_byMaxRound;
	int m_gameDiFen;
	int m_qipai;
	int m_byMaxUser;
	int m_wFaPaiDelay; // 发牌延迟

	int m_wRobotSeconds[] = new int[5]; // 等待时长

	public int m_byZhuang;// 当前庄位置

	public int m_byQiangZhuang[] = new int[5]; // 是否抢庄，和抢庄的倍率

	public int m_byCostGem;
	public int m_nGiveUpOrder; // 弃牌人的先后顺序
	public String m_payWay;
	public String m_fapaiWay;

	public String m_zhuangMode;// 选庄，抢庄，轮庄
	public String m_guize; // 算分规则
	public String m_xifen; //喜分

	public String m_strParam;
	public String m_strRoomDesc;
	public String m_vecInputWenZi;
	public String[] m_vecParam;

	public int m_vecWinScore[] = new int[5];
	public int m_vecLoseScore[] = new int[5];
	public int m_vecDetailGroupScore[][] = new int[5][5]; // 每一道牌赢的次数	
	
	public int m_vecRoundScore[] = new int[5];
	public int m_vecResultScore[] = new int[5];
	public double m_doubleVecResultScore[] = new double[5];
	public double m_doubleKouWinResultScore[] = new double[5];
	public int m_kouWinSumJu = 0;
	public int m_kouWinCurrJu = 0;

	public int m_vecIsKaiPaiReady[] = new int[5];
	public int m_vecIsGiveUp[] = new int[5];
	public int m_vecQiPaiPlayer[] = new int[5]; // 弃牌先后顺序
	
	public BJRoundRecord m_vecRoundRecord[] = new BJRoundRecord[88];
	
	GameLogic m_GameLogic = new GameLogic();

	String m_curStage;

	int m_bTableCardArray[][] = new int[5][9]; // 桌面扑克 //been 4
	int m_cbHandCardData[][] = new int[5][9];
	int m_locked = 0;// 默认允许进
	int m_nPeiPaiCountDown; // 配牌倒计时
	int m_lLookOnLeaveDownTime; // 旁观者倒计时 

	IServerUserItem m_pIUserItem[] = new IServerUserItem[5]; // 用户指针

	{
		for (int i = 0; i < m_pIUserItem.length; i++) {
			m_pIUserItem[i] = new IServerUserItem();
		}
	}

	int m_LookOnUserID[] = new int[50]; // 观众

	public int m_Creator;// 房主

	public int m_createTime = 0;
	BJZTable() {
		m_createTime = (int)(System.currentTimeMillis() / 1000);
		m_bTableStarted = false;
		m_lStartTime = System.currentTimeMillis() / 1000;
		m_bRemove = false;
		m_byCostGem = 0;
		m_payWay = "FZPAY";
		m_fapaiWay = "1ZHANG";

		m_zhuangMode = "WUZHUANG";
		m_guize = "GUIZE_HBL";
		m_xifen = "JDXIFEN"; // 默认经典喜分
        m_qipai=1;
        m_gameDiFen=1;
       
        m_nGiveUpOrder = 0;
		m_byMaxUser = 5;
	}

	void Init(int cbReason) {
		m_bPeiPaiReady = false;
		m_bLookOnDownTimeStart = false;
		m_nPeiPaiCountDown = 0;
		m_lLookOnLeaveDownTime = 0;
		m_vecInputWenZi="";	

		m_dwLastDoCmdTime = 0;
		m_bGameStarted = false;
		m_wFaPaiDelay = 0;
		m_nGiveUpOrder = 0;


		for (int i = 0; i < 5; i++) {
			m_wRobotSeconds[i] = 0;

			m_byQiangZhuang[i] = 0;
			m_vecIsKaiPaiReady[i] = 0;
			m_vecIsGiveUp[i] = 0;
			m_vecQiPaiPlayer[i] = 0;
			m_vecQiPaiPlayer[i] = 0;
			m_vecRoundScore[i] = 0;
			
			for (int j = 0; j < 9; j++) {
				m_cbHandCardData[i][j] = 0;
			}
		}
		switch (cbReason) {
		case Constants.INIT_TOTAL: {
			m_dismissTimer = 0;
			m_byAgreeDismiss[0] = 0;
			m_byAgreeDismiss[1] = 0;
			m_byAgreeDismiss[2] = 0;
			m_byAgreeDismiss[3] = 0;

			Constants.memset(m_LookOnUserID, 0, Constants.sizeof(m_LookOnUserID)); //

			m_byRound = 0;
			m_byMaxRound = 0;
			m_byZhuang = 0;
			for (int i = 0; i < 5; i++) {
				m_vecWinScore[i] = 0;
				m_vecLoseScore[i] = 0;
				m_vecResultScore[i] = 0;
				m_doubleVecResultScore[i] = 0;
			}
		}
			break;
		case Constants.INIT_FIRST_ROUND: {

		}
			break;
		case Constants.INIT_NEWROUND: {

		}
			break;
		}
	}

	void KickRobots() {
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {

			}
		}
	}

	boolean HasRobot() {
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				return true;
			}
		}
		return false;
	}

	void SendAvatar(int _Uid) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}

		String iter = dthall.getInstance().Handle_OnGetUserAvatar(_Uid);

		SUBCMD_S_Avatar cmd = new SUBCMD_S_Avatar();
		cmd.m_lUid = _Uid;
		if (iter != null) {
			cmd.szAvatarURL = iter;
		} else {
			cmd.szAvatarURL = "http://main.lfyl.world/lfyl80.png";
		}

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}
	
	public void SendLocationData(SHMUser user){
		JSONObject data = new JSONObject();
		data.put("loc",user.getGpsLocInfo());
		data.put("uid", user.getUid());
		data.put("command","GPS_LOC");
		SendData(Constants.INVALID_CHAIR,data.toString());
		
		for(short i = 0;i < m_pIUserItem.length;i++){
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_pIUserItem[i].isAndroidUser == 0) {
					int uid = m_pIUserItem[i].dwUserID;
					SHMUser other = dthall.getInstance().getUserInfo(uid);
					if(other != null){
						data.put("uid", other.getUid());
						data.put("loc",other.getGpsLocInfo());
						dthall.getInstance().onSend(user.getUid(),data.toString());
					}
				}
			}
		}
	}

	public void SendGpsData(SHMUser user){
		JSONObject data = new JSONObject();
		List<Double> gpsData = user.getLocateInfo();
		data.put("lng", gpsData.get(0));
		data.put("lat", gpsData.get(1));
		data.put("command","GPS_DATA");
		data.put("uid",user.getUid());
		SendData(Constants.INVALID_CHAIR,data.toString());
		
		for (short i = 0; i < m_pIUserItem.length; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_pIUserItem[i].isAndroidUser == 0) {
					int uid = m_pIUserItem[i].dwUserID;
					SHMUser other = dthall.getInstance().getUserInfo(uid);
					if(other != null){
						gpsData = other.getLocateInfo();
						data.put("uid", other.getUid());
						data.put("lng",gpsData.get(0));
						data.put("lat",gpsData.get(1));
						dthall.getInstance().onSend(user.getUid(),data.toString());
					}
				}
			}
		}
	}
	public void OnUserSit(int _Uid) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbOnline = Constants.US_NULL;
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if (user != null) {
			SendGpsData(user);
			SendLocationData(user);
		}
		if (user != null) {
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID == _Uid) {
					m_wRobotSeconds[i] = 0;
					m_pIUserItem[i].dwUserID = _Uid;
					// m_pIUserItem[i].lUserScore = user.getCoin();
					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);
					m_pIUserItem[i].lUserCoin = user.getCoin(0);
					if(this.isDwcType()){
						m_pIUserItem[i].lUserRmb = user.getRmb1v1Value();
					}

					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
					// Constants.memset(&cmd, 0, Constants.sizeof(cmd));

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.szIP = user.getIP(null);
					cmd.m_nState = m_pIUserItem[i].cbUserStatus;
					if(this.isGoldType())
						cmd.m_lUserCoin = m_pIUserItem[i].lUserCoin;
					else if(this.isDwcType())
						cmd.m_lUserCoin = m_pIUserItem[i].lUserRmb;
					
					SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					SendAvatar(m_pIUserItem[i].dwUserID);

					return;
				}
			}
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID == 0) {
					m_wRobotSeconds[i] = 0;
					m_pIUserItem[i].dwUserID = _Uid;
					m_pIUserItem[i].lUserScore = 0;
					m_pIUserItem[i].wChairID = i;
					m_pIUserItem[i].dwFaceID = 0;
					m_pIUserItem[i].cbGender = user.getSex();
					m_pIUserItem[i].szNickName = user.getNickName(null);
					m_pIUserItem[i].szIP = user.getIP(null);
					m_pIUserItem[i].lUserCoin = user.getCoin(0);
					if(this.isDwcType()){
						m_pIUserItem[i].lUserRmb = user.getRmb1v1Value();
					}
					
					if(m_bTableStarted)
					{
						m_pIUserItem[i].cbUserStatus = Constants.US_LOOKON;
					}
					
					SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
					// Constants.memset(&cmd, 0, Constants.sizeof(cmd));

					cmd.m_lUid = _Uid;
					cmd.m_wUserChairID = m_pIUserItem[i].wChairID;
					cmd.m_dwTableID = m_dwTableID;
					cmd.m_lUserScore = m_pIUserItem[i].lUserScore;
					cmd.m_cbGender = m_pIUserItem[i].cbGender;
					cmd.m_iAvatar = m_pIUserItem[i].dwFaceID;
					cmd.szNickName = user.getNickName(null);
					cmd.m_nState = m_pIUserItem[i].cbUserStatus;
					cmd.szIP = user.getIP(null);
					if(this.isGoldType()){
						cmd.m_lUserCoin = m_pIUserItem[i].lUserCoin;
					}else if(this.isDwcType()){
						cmd.m_lUserCoin = m_pIUserItem[i].lUserRmb;
					}
					
					SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

					if (m_bGameStarted) {
						SendGameScene(_Uid, Constants.GS_PLAYING);
					} else {
						SendGameScene(_Uid, Constants.GS_FREE);
					}

					return;
				}
			}
		}

		return;
	}

	int FindSeat(long _Uid) {
		for (int bySeatID = 0; bySeatID < 5; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}

	int GetPlayerCount() {
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	int GetRobotCount() {
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].isAndroidUser == 1) {
				cntPlayer++;
			}
		}
		return cntPlayer;
	}

	public void OnUserStandUp(int _Uid,boolean leaveroom) {

		int wChairID = FindSeat(_Uid);
		
		
		if(m_bTableStarted == true && this.isDwcType() && leaveroom == true){
			//真金情况可以退出
			SUBCMD_S_StandUp cmd = new SUBCMD_S_StandUp();
			cmd.m_lUid = _Uid;
			cmd.m_wUserChairID = wChairID;
			cmd.m_dwTableID = m_dwTableID;
			cmd.leaveRoom = leaveroom ?1:0;
			
			for(int i = 0; i < 5; i ++){
				if(m_pIUserItem[i].dwUserID > 0 && _Uid != m_pIUserItem[i].dwUserID)
				{
					SendData(m_pIUserItem[i].dwUserID, cmd.getJsonData());
				}
			}
			
			m_pIUserItem[wChairID].Init();
			QpGameMgr.getInstance().OnRemoveUserRoom(_Uid);
			//检查是否全部是机器人
			int realCnt = 0;
			for(int i = 0; i < 5; i ++){
				if(m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0)
				{
					realCnt++;
				}
			}
			if(realCnt == 0){
				OnEventGameEnd(0, Constants.GAME_END_DISMISS);
				return;
			}
			//是否全部准备好了
			{
				int cntReady = 0;
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].cbUserStatus == Constants.US_READY) {
						cntReady++;
					}
				}
			
				int cntUser = 0;
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus != Constants.US_LOOKON) {
						cntUser++;
					}
				}
			
				if (cntReady == cntUser && cntReady > 1) {
					OnEventGameStart();
				}
			}
			return;
		}
		if((Constants.US_LOOKON ==  m_pIUserItem[wChairID].cbUserStatus) 
				|| (m_bTableStarted == false && wChairID != 0 && leaveroom == true)){
			SUBCMD_S_StandUp cmd = new SUBCMD_S_StandUp();
			cmd.m_lUid = _Uid;
			cmd.m_wUserChairID = wChairID;
			cmd.m_dwTableID = m_dwTableID;
			cmd.leaveRoom = leaveroom ?1:0;
			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			QpGameMgr.getInstance().removeUserRoom(_Uid);
			m_pIUserItem[wChairID].Init();
//			if(m_bTableStarted)
			{
				int cntReady = 0;
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].cbUserStatus == Constants.US_READY) {
						cntReady++;
					}
				}
				
				int cntUser = 0;
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus != Constants.US_LOOKON) {
						cntUser++;
					}
				}
				
				if (cntReady == cntUser && cntReady > 1) {
					OnEventGameStart();
				}
			}
		}

		if ((Constants.US_LOOKON ==  m_pIUserItem[wChairID].cbUserStatus) || (m_bTableStarted == false && leaveroom == true)) {
			if (wChairID > 0 && wChairID < 5) {
				m_pIUserItem[wChairID].Init();
				QpGameMgr.getInstance().removeUserRoom(_Uid);
			} else {
			}
		} else {
			if (wChairID >= 0 && wChairID < 5) {
				m_pIUserItem[wChairID].cbOnline = (int) Constants.US_OFFLINE;
			}
		}
	}

	public void SetParam(String szParam) {
		m_strParam = szParam;

		m_vecParam = m_strParam.split(";");
	}

	boolean SendGameScene(int _Uid, int bGameStatus) {
		if (m_strParam.length() > 0) {
			SUBCMD_S_Param cmd = new SUBCMD_S_Param();
			cmd.m_dwTableID = m_dwTableID;
			cmd.szParam = m_strParam;
			if(this.isDwcType() && this.m_dwcKouYing != 0){
				cmd.dwcKouYingRate = m_dwcKouYingRate;
			}
			cmd.m_kouYingJuShu = m_kouWinSumJu;
			SendData(_Uid, cmd.getJsonData());
		}
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		{
			SUBCMD_S_Table cmd = new SUBCMD_S_Table();
			cmd.vecInputWenZi = m_vecInputWenZi;
			cmd.m_dwTableID = m_dwTableID;// 房间ID
			cmd.m_bGameStarted = m_bGameStarted ? 1 : 0;// 是否开始
			cmd.m_bTableStarted = m_bTableStarted ? 1 : 0;// 是否开始
			cmd.m_byRound = m_byRound;
			cmd.m_byMaxRound = m_byMaxRound;
			cmd.m_cntUser = cntUser;// 玩家
			cmd.m_cntLookon = 1;// 观众
			Constants.memset(cmd.m_seatInfo, 0, Constants.sizeof(cmd.m_seatInfo));

			for (int i = 0; i < 5; i++) {
				cmd.m_seatInfo[i].m_lUid = m_pIUserItem[i].dwUserID;// UID
				cmd.m_seatInfo[i].m_iTotalCoin = m_pIUserItem[i].lUserScore;// COIN
				cmd.m_seatInfo[i].m_NickName = m_pIUserItem[i].szNickName;// NICKNAME
				cmd.m_seatInfo[i].m_iBetCoin = 0;//
				cmd.m_seatInfo[i].m_cbGender = m_pIUserItem[i].cbGender;
				cmd.m_seatInfo[i].m_iAvatar = m_pIUserItem[i].dwFaceID;//
				cmd.m_seatInfo[i].m_cbUserStatus = m_pIUserItem[i].cbUserStatus;// 用户状态
				cmd.m_seatInfo[i].m_iVipLevel = m_pIUserItem[i].cbVipLevel;// VIP
																			// LEVEL
				cmd.m_seatInfo[i].m_iTitle = 0;// 称号
				cmd.m_seatInfo[i].m_isLookon = 0;// 是否观众
				cmd.m_seatInfo[i].szIP = m_pIUserItem[i].szIP;// NICKNAME
				if(this.isGoldType())
					cmd.m_seatInfo[i].m_lTotalCoin = m_pIUserItem[i].lUserCoin;
				else
					cmd.m_seatInfo[i].m_lTotalCoin = m_pIUserItem[i].lUserRmb;
			}

			SendData(_Uid, cmd.getJsonData());
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID > 0) {
					SendAvatar(m_pIUserItem[i].dwUserID);
				}
			}
		}

		if (m_bTableStarted == true) {
			{
				SUBCMD_S_STAGE cmd = new SUBCMD_S_STAGE();

				cmd.szStage = m_curStage;
				cmd.needDealTime = (int) needDealTime;
				for (int i = 0; i < 5; i++) {
					for (int k = 0; k < 9; k++) {
						cmd.m_bTableCardArray[i][k] = m_bTableCardArray[i][k]; // 桌面扑克
					}
				}

				SendData(_Uid, cmd.getJsonData());
				
				
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].dwUserID > 0) {
						
					}
				}
				
				for(int i = 0; i < m_vecRoundRecord.length; i ++)
				{
					if(null == m_vecRoundRecord[i]){
						continue;
					}
					
					SUBCMD_S_ROUND_RECORD cmd_Record = new SUBCMD_S_ROUND_RECORD();
					cmd_Record.m_vecRecordRoundPai = m_vecRoundRecord[i].m_vecRecordRoundPai;
					cmd_Record.m_vecRecordRoundScore = m_vecRoundRecord[i].m_vecRecordRoundScore;
					cmd_Record.m_vecRecordRoundScoreDetail = m_vecRoundRecord[i].m_vecRecordRoundScoreDetail;
					cmd_Record.m_vecRecordRoundXifen = m_vecRoundRecord[i].m_vecRecordRoundXifen;
					cmd_Record.m_vecPlayerChairId = m_vecRoundRecord[i].m_vecPlayerChairId;
					cmd_Record.m_names = m_vecRoundRecord[i].m_names;
					cmd_Record.m_nRound = i;
					
					String sData_record = cmd_Record.getJsonData();
					SendData(_Uid, sData_record);
				}
			}

			if (m_bGameStarted == true) {
				{
					SUBCMD_S_TableBegin cmd = new SUBCMD_S_TableBegin();

					cmd.byRound = m_byRound;
					cmd.byMaxRound = m_byMaxRound;
					cmd.nPeiPaiCountDown = Constants.PEIPAI_COUNTDOWNTIME * 1000 - m_nPeiPaiCountDown;
					 
					for (int i = 0; i < 5; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							cmd.bIsReady[i] = m_pIUserItem[i].cbUserStatus;
							cmd.bIsKaiPaiReady[i] = m_vecIsKaiPaiReady[i];
						}
					}
					
					for (int i = 0; i < 5; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							cmd.bIsGiveUP[i] = m_vecIsGiveUp[i];
						}
					}
					
					int seat = FindSeat(_Uid);
					
					if(1 == m_vecIsKaiPaiReady[seat])
					{
						cmd.byPeiPai = m_cbHandCardData[seat];
					}
					
					SendData(_Uid, cmd.getJsonData());
				}
				{

					for (int i = 0; i < 5; i++) {
						if (m_pIUserItem[i].dwUserID > 0) {
							SUBCMD_S_TableSide cmd = new SUBCMD_S_TableSide();
							cmd.bySeatID = i;
							cmd.bIsReady = m_pIUserItem[i].cbUserStatus;
							cmd.bIsKaiPaiReady = m_vecIsKaiPaiReady[i];
							for (int k = 0; k < 9; k++) {
								cmd.byPai[k] = m_bTableCardArray[i][k];
							}
							int wChairID = FindSeat(_Uid);
							SendData(_Uid, cmd.getJsonData());
						}
					}
				}
				{
					SUBCMD_S_TableEnd cmd = new SUBCMD_S_TableEnd();

					SendData(_Uid, cmd.getJsonData());
				}
			}
		}

		switch (bGameStatus) {
		case Constants.GS_FREE: // 空闲状态
		{
			// 构造数据

			return true;
		}
		case Constants.GS_PLAYING: // 游戏状态
		{
			// 构造数据

			return true;
		}
		}

		return true;
	}

	void FaPai() {
		m_bTableStarted = true;
		m_bGameStarted = true;
		m_bPeiPaiReady = true;
		
		m_curStage = "STAGE_FAPAI";

		needDealTime = 8;

		if (m_byRound == 0) {
			m_byRound = 1;
		}
		OnRoundInfoChange();
		int cbTableCardArray[] = new int[52];
		//TODO
		int test[] = new int[9];
		for(int i=0;i<9;i++){
			test[i] = i+1;
		}
		m_GameLogic.RandCardList(cbTableCardArray, 52); // 计算数组的个数
		for (int i = 0; i < 45; i++) {
			int x = i / 9;
			int y = i % 9;
			m_bTableCardArray[x][y] = cbTableCardArray[i];
			//m_bTableCardArray[0][y] = test[y];
		}
		
			  //   /*1 ~ 13*/ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,	//方块 A - K
			 //   /*17 ~ 29*/ 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D,	//梅花 A - K
			 //   /*33 ~ 45*/ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,	//红桃 A - K
			 //   /*49 ~ 61*/ 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D,	//黑桃 A - K
		// zuopai
		int [][] testArray = {
							//{1,2,3,4,5,6,7,8,9},//娑撳銆庨棃锟�
//							{10,21,51,4,57,58,42,45,46},// A閵嗭拷2閵嗭拷3
													
							{ 0x01, 0x11, 0x21, 0x31, 0x27, 0x29, 0x37, 0x39, 0x36},
							{0x32, 0x33, 0x34, 0x15, 0x16, 0x17, 0x19, 0x1A, 0x11},
							{0x07,0x17,0x27,0x08,0x18,0x28,0x09,0x19,0x29},
							{0x0D, 0x1D, 0x2D,0x3D,0x0C,0x1C,0x2C, 0x01, 0x02},
							{0x22, 0x23, 0x24,0x05,0x15,0x25,0x06,0x16,0x26}
						};
		
//		m_bTableCardArray = testArray;
//		m_bTableCardArray[0] = testArray[0];
//		m_bTableCardArray[1] = testArray[1];
		
		System.out.println("FAPAI : " + m_dwTableID + "_" + m_byRound);
		// System.out.println(IJsonExport.sprintf_BYTE(cbTableCardArray, 52));

		for (int i = 0; i < 5; i++) {
			// System.out.println("POS: " + i);
			// System.out.println(IJsonExport.sprintf_BYTE(m_bTableCardArray[i],
			// 13));
		}
		SUBCMD_S_FaPai cmd = new SUBCMD_S_FaPai();
		cmd.byRound = m_byRound;
		cmd.byMaxRound = m_byMaxRound;
		cmd.m_nPeiPaiCountDown = Constants.PEIPAI_COUNTDOWNTIME;
		
		for (int i = 0; i < 5; i++) {
			m_vecRoundScore[i] = 0;
			if (m_pIUserItem[i].dwUserID > 0 && (m_pIUserItem[i].cbUserStatus == Constants.US_READY
					|| m_pIUserItem[i].cbUserStatus == Constants.US_PLAY)) {
				m_pIUserItem[i].cbUserStatus = Constants.US_PLAY;
				cmd.vecIsReady[i] = 1;
			} else {
				// 旁观状态不能清空
				if(Constants.US_LOOKON !=  m_pIUserItem[i].cbUserStatus)
				{
					m_pIUserItem[i].cbUserStatus = Constants.US_NULL;
				}
				
				cmd.vecIsReady[i] = 0;
			}
		}

		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == Constants.US_PLAY) {
				for (int k = 0; k < 9; k++) {
					cmd.byPai[k] = m_bTableCardArray[i][k];
//					if(i == 0){
//						cmd.byPai[k] = testArray[12][k];
//						m_bTableCardArray[i][k] = cmd.byPai[k];
//					}
				}

				SendData(m_pIUserItem[i].dwUserID, cmd.getJsonData());
			}
		}
	}

	public static int GetNextUser(int bySeatID) {
		return (bySeatID + 1) % 5;
	}

	void KaiPai_HBL() {
		int cntUser = 0;
		int realUser = 0; // 排除弃牌人数
		int userArray[] = {-1,-1,-1,-1,-1}; // 存储牌桌上玩家的座位号
		// 战绩记录实例
		BJRoundRecord recordObj = new BJRoundRecord();
		recordObj.initData();
		
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == 2) {
				if (0 == m_vecQiPaiPlayer[i]) {
					realUser ++;
				}
				
				recordObj.m_vecPlayerChairId[i] = FindSeat(m_pIUserItem[i].dwUserID);
				recordObj.m_names[i] = m_pIUserItem[i].szNickName;
				userArray[cntUser] = FindSeat(m_pIUserItem[i].dwUserID);
				cntUser ++;
			}
		}
		
		int[][][] cardGroupArray = new int[3][][];
		
		// 遍历存储同一道牌中所有人的牌集合
		// 结构：[[第一道牌],[第二道牌]] 每个玩家每道牌[0,0,0,1]第四个'1'是座位号
		for (int groupIdx = 0; groupIdx < 3; groupIdx ++)
		{
			int[][] cardGroupItemArray = new int[realUser][];
			int index = 0;
			for(int seat = 0; seat < cntUser; seat ++)
			{
				int chairID = userArray[seat];
				if(0 == m_cbHandCardData[chairID][0])
				{
					continue;
				}
				
				for(int i = 0; i < m_cbHandCardData[chairID].length;i ++)
				{
					// 记录每一局的牌值
					recordObj.m_vecRecordRoundPai[chairID][i] = m_cbHandCardData[chairID][i];
				}
				
				
				int cardArray[] = new int[4];
				for (int i = 0; i < 3 ; i ++ ) 
				{
					cardArray[i] = m_cbHandCardData[chairID][groupIdx * 3 + i];
				}
				
				cardArray[3] = chairID;
				m_GameLogic.SortCardList(cardArray,3);
				cardGroupItemArray[index] = cardArray;
				index ++;
			}

			cardGroupArray[groupIdx] = cardGroupItemArray;
		}
		
		// 冒泡排序
		for (int i = 0; i < cardGroupArray.length; i ++ ) {
			int[] tmpCard = new int[4];
			for (int j = 0; j < cardGroupArray[i].length; j++) {
				for (int k = 0; k < cardGroupArray[i].length-1-j; k++) {
					try {
						int ret = m_GameLogic.CompareCard(cardGroupArray[i][k], cardGroupArray[i][k + 1], 3);
						
						if (2 == ret){
							tmpCard = cardGroupArray[i][k];
							cardGroupArray[i][k] = cardGroupArray[i][k + 1];
							cardGroupArray[i][k + 1] = tmpCard;
						}
					} catch (Exception e) {
						System.out.println("bj Error log: type ");
						for (int m = 0; m < cardGroupArray.length; m ++ ){
							for (int n = 0; n < cardGroupArray[m].length; n ++ ){
								System.out.println(Arrays.toString(cardGroupArray[m][n]));
							}
						}
						
					}
					
				}
			}
		}

		//////////////////////  统计分数  ///////////////////////
		
		//先根据底分计算再判断喜
		// 结构[[第一道牌分数,第二道牌分数,第三道牌分数,喜分的分数]] 
		int[][] scoreArray = new int[5][4];
		
		if (cntUser > realUser) {
			// int index = 0;
			// // 筛选出没有弃牌的玩家
			// int noGiveUpPlayer[] = new int[realUser];
			// for (int i = 0;i < userArray.length ;i++ ) {
			// 	if (-1 != userArray[i] && 0 == m_vecQiPaiPlayer[userArray[i]]) {
			// 		noGiveUpPlayer[index] = userArray[i];
			// 		index ++;
			// 	}
			// }

			//有人弃牌
			if (0 != realUser) {
				// 先处理没有弃牌玩家的分数
				for(int i = 0; i < 3; i ++)
				{
					int count= cntUser;
					for(int j = 0; j < realUser ; j ++)
					{
						if(j == realUser - 1)
						{	
						// 赢其他几家的分数总和
							for (int k = cntUser - 1;k > 0 ; k-- ) {
								scoreArray[cardGroupArray[i][j][3]][i] += k * m_gameDiFen;
							}

						// 统计每一道牌赢的次数
							m_vecDetailGroupScore[cardGroupArray[i][j][3]][i] += 1;
						}else
						{
							scoreArray[cardGroupArray[i][j][3]][i] -= (count - (cntUser - realUser) - 1) * m_gameDiFen;
						}

						count --;
					}
				}

				int tmpArray[] = {-1,-1,-1,-1,-1};

				// 按照弃牌顺序排序
				for (int chairID = 0; chairID < m_vecQiPaiPlayer.length; chairID ++ ) {
					if (m_vecQiPaiPlayer[chairID] > 0) {
						tmpArray[m_vecQiPaiPlayer[chairID] - 1] = chairID;
					}
				}

				int count = realUser;
				if (0 == count) {
					count = 1;
				}

				for (int i = 0; i < tmpArray.length ; i ++) {
					if (-1 != tmpArray[i]) {
						for (int j = 0; j < 3; j ++ ) {
							scoreArray[tmpArray[i]][j] -= count * m_gameDiFen;
						}

						count ++;
					}
				}
			}
		}else{
			// 无人弃牌
			for(int i = 0; i < 3; i ++)
			{
				int count= cntUser;
				for(int j = 0; j < cntUser ; j ++)
				{
					if(j == cntUser - 1)
					{	
					    // 赢其他几家的分数总和
						for (int k = cntUser - 1;k > 0 ; k-- ) {
							scoreArray[cardGroupArray[i][j][3]][i] += k * m_gameDiFen;
						}

					    // 统计每一道牌赢的次数
						m_vecDetailGroupScore[cardGroupArray[i][j][3]][i] += 1;
					}else
					{
						scoreArray[cardGroupArray[i][j][3]][i] -= (count - 1) * m_gameDiFen;
					}

					count --;
				}
			}
		}
		
		
		// 再算喜分
		int [][] xifen = new int[realUser][];
		// 重组玩家手牌
		int [][][] playCardGroupList = new int [5][3][];
		
		for(int i = 0; i < 3; i ++)
		{
			for(int j = 0; j < cntUser ; j ++)
			{
				if(null !=cardGroupArray[i] && j < cardGroupArray[i].length && null !=cardGroupArray[i][j])
				// 统计玩家每道的牌
				playCardGroupList[cardGroupArray[i][j][3]][i] = cardGroupArray[i][j];
			}
		}
		
		String [] xifenTypeList = new String[5];// 用来存储喜分类型和分数
	
		// 判断喜分
		int chairID = 0;
		for (int seat = 0 ;seat < cntUser ;seat ++ )
		 {
		 	chairID = userArray[seat];
		 	if (m_vecQiPaiPlayer[chairID] > 0) {
		 		// chairID ++; 
		 		continue;
		 	}

			String xifenTypeScore = ""; 
			
			// '通关' 判断
			boolean isTongGuan = true;
			for (int i = 0; i < 3 ;i ++ ) {
				// 判断三道牌最后一个位置是不是该玩家
				if (chairID != cardGroupArray[i][realUser - 1][3])
				{
					isTongGuan = false;
				}
			}
			
			int score = 0; // 统计总得分

			// 通关
			if(isTongGuan)
			{
				int tmpScore = 0;
				if("JDXIFEN" == m_xifen)//经典喜分
				{
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 2; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 2; 
					}
				}
				else if("GDXIFEN" == m_xifen)//固定喜分
				{
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += 1; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += 2; 
					}
				}

				score += tmpScore;
				xifenTypeScore += ",tongguan:" + tmpScore * (realUser - 1);

				// 统计通关的次数
				m_vecDetailGroupScore[chairID][4] += 1;
			}
			
			int quanhei = m_GameLogic.isPaiXingQuanHei(playCardGroupList[chairID],3);
			int quanhong = m_GameLogic.isPaiXingQuanHong(playCardGroupList[chairID],3);
			//全黑/全红
			if (0 != quanhei|| 0 != quanhong)
			{
				int tmpScore = 0;
				if("JDXIFEN" == m_xifen)//经典喜分
				{
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 2; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 2; 
					}
				}
				else if("GDXIFEN" == m_xifen)//固定喜分
				{
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += 3; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += 3; 
					}
				}

				score += tmpScore;
				// 记录喜分类型和分数
				if (0 != quanhei) {
					xifenTypeScore += ",quanhei:";
				}else{
					xifenTypeScore += ",quanhong:";
				}

				xifenTypeScore += tmpScore * (realUser - 1);
				
				// 统计喜分的次数
				m_vecDetailGroupScore[chairID][3] += 1;
			}
			
			int sanQing = m_GameLogic.isPaiXingSanQing(playCardGroupList[chairID],3);//三青
			int sanShunZi = m_GameLogic.isPaiXingQuanShunZi(playCardGroupList[chairID],3);//三顺子
			int shuangShunQing = m_GameLogic.isPaiXingShuangShunQing(playCardGroupList[chairID],3);//双顺青
			int shuangSanTiao = m_GameLogic.isPaiXingShuangSanTiao(playCardGroupList[chairID],3);//双三条
			
			int sanShunQing = m_GameLogic.isPaiXingSanShunQing(playCardGroupList[chairID],3);//三顺青
			int lianShunQing = m_GameLogic.isPaiXingLianShunQing(playCardGroupList[chairID],3);//连顺青
			int quanSanTiao = m_GameLogic.isPaiXingQuanSanTiao(playCardGroupList[chairID],3);//全三条
			int lianShun =  m_GameLogic.isPaiXingSanLianShun(playCardGroupList[chairID],3);//连顺
			int zhaDan = m_GameLogic.isPaiXingSiTiao(playCardGroupList[chairID],3);//炸弹
			int shuangZhaDan = m_GameLogic.isPaiXingShuangSiTiao(playCardGroupList[chairID],3);//双炸弹
			
			sanShunZi = 0;
			//三顺青/连顺青/连顺/全三条/双炸弹 * 3倍
			if ( 0!= sanShunQing
				|| 0!= lianShunQing
				|| 0!= lianShun
				|| 0!= quanSanTiao
				|| 0!= shuangZhaDan)
			{
				sanQing = 0;
				sanShunZi = 0;
				shuangShunQing = 0;
				shuangSanTiao = 0;

				int tmpScore = 0;
				
				if("JDXIFEN" == m_xifen)//经典喜分
				{
					int nBei = 3;
					
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += (cntUser-1) * m_gameDiFen * nBei; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += (cntUser-1) * m_gameDiFen * nBei; 
					}
				}
				else if("GDXIFEN" == m_xifen)//固定喜分
				{
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += 6; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += 12; 
					}
				}
								
				// 记录喜分类型和分数
				if(0 != shuangZhaDan){
					xifenTypeScore += ",shuangzhadan:";
				}else if(0 != quanSanTiao){
					xifenTypeScore += ",quansantiao:";
				}else if(0 != lianShunQing){
					xifenTypeScore += ",lianshunqing:";
				}else if(0 != lianShun){
					xifenTypeScore += ",lianshun:";
				}else if(0 != sanShunQing){
					xifenTypeScore += ",sanshunqing:";
				}
				
				score += tmpScore;
				xifenTypeScore += tmpScore * (realUser - 1);

				// 统计喜分的次数
				m_vecDetailGroupScore[chairID][3] += 1;
			}
			
			if (0 != shuangZhaDan) {
				zhaDan = 0;
			}

			//双三条 * 1倍
			if (0!= shuangSanTiao)
			{
				int tmpScore = 0;
				
				if("JDXIFEN" == m_xifen)//经典喜分
				{	
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 1; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 1; 
					}
				}
				else if("GDXIFEN" == m_xifen)//固定喜分
				{
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += 3; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += 6; 
					}
				}

				// 鐠佹澘缍嶉崰婊冨瀻缁鐎烽崪灞藉瀻閺侊拷
				xifenTypeScore += ",shuangsantiao:";
				
				score += tmpScore;
				xifenTypeScore += tmpScore * (realUser - 1);
				// 统计喜分的次数
				m_vecDetailGroupScore[chairID][3] += 1;
			}
			

			//炸弹  * 1倍
			if (0!= zhaDan)
			{
				int tmpScore = 0;
				
				if("JDXIFEN" == m_xifen)//经典喜分
				{	
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 1; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 1; 
					}
				}
				else if("GDXIFEN" == m_xifen)//固定喜分
				{
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += 3; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += 6; 
					}
				}

				// 鐠佹澘缍嶉崰婊冨瀻缁鐎烽崪灞藉瀻閺侊拷
				xifenTypeScore += ",zhadan:";
				
				score += tmpScore;
				xifenTypeScore += tmpScore * (realUser - 1);
				// 统计喜分的次数
				m_vecDetailGroupScore[chairID][3] += 1;
			}

			//三青  * 1倍
			if (0!= sanQing)
			{
				int tmpScore = 0;
				
				if("JDXIFEN" == m_xifen)//经典喜分
				{	
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 1; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 1; 
					}
				}
				else if("GDXIFEN" == m_xifen)//固定喜分
				{
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += 3; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += 6; 
					}
				}

				// 鐠佹澘缍嶉崰婊冨瀻缁鐎烽崪灞藉瀻閺侊拷
				if(0 != sanQing){
					xifenTypeScore += ",sanqing:";
				}
				
				score += tmpScore;
				xifenTypeScore += tmpScore * (realUser - 1);
				// 统计喜分的次数
				m_vecDetailGroupScore[chairID][3] += 1;
			}

			//双顺青  * 1倍
			if (0!= shuangShunQing)
			{
				int tmpScore = 0;
				
				if("JDXIFEN" == m_xifen)//经典喜分
				{	
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 1; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += (cntUser-1) * m_gameDiFen * 1; 
					}
				}
				else if("GDXIFEN" == m_xifen)//固定喜分
				{
					if(1 == m_gameDiFen)// 底分
					{
						tmpScore += 3; 
					}else if(2 == m_gameDiFen)
					{
						tmpScore += 6; 
					}
				}

				// 鐠佹澘缍嶉崰婊冨瀻缁鐎烽崪灞藉瀻閺侊拷
				if(0 != shuangShunQing){
					xifenTypeScore += ",shuangshunqing:";
				}
				
				score += tmpScore;
				xifenTypeScore += tmpScore * (realUser - 1);
				// 统计喜分的次数
				m_vecDetailGroupScore[chairID][3] += 1;
			}			
			
			// 记录每一局的喜分
			recordObj.m_vecRecordRoundXifen[chairID] = xifenTypeScore;
			
			xifenTypeList[chairID] = xifenTypeScore;
			// 结算分数
			for (int i = 0;i < cntUser ;i ++ ) {
				int chairID_ = userArray[i];
				if(chairID_ == chairID)
				{
					// 自己加分
					scoreArray[chairID_][3] += score * (realUser - 1);
				}else{
					// 排除弃牌人数
					if(0 == m_vecQiPaiPlayer[chairID_]){
						// 其他人减分
						scoreArray[chairID_][3] -= score;
					}					
				}
			}

			// chairID ++; 
		}
		
	    SUBCMD_S_SuanFen cmd = new SUBCMD_S_SuanFen();
	    cmd.xifenTypeList = xifenTypeList;
	    
		for (int i = 0; i < 5; i++) {
			int allScore = 0;
			
			for (int j = 0; j < 4; j++) {
				cmd.nScore[i][j] = scoreArray[i][j];
				allScore += scoreArray[i][j];
                if(j < 3)
                {
                	// 记录每一局每个人三道牌的牌值
                	recordObj.m_vecRecordRoundScoreDetail[i][j] = scoreArray[i][j];
                }
				// 记录每一局总分数
				m_vecRoundScore[i] = allScore;
			}
		}
		
		for(int i = 0; i < m_vecRoundScore.length; i ++)
		{
			// 记录每一局每个人每一局的总分数
			recordObj.m_vecRecordRoundScore[i] = m_vecRoundScore[i];
		}
		
		
		m_vecRoundRecord[m_byRound - 1] = recordObj;
		
		String sData = cmd.getJsonData();
		
		SUBCMD_S_ROUND_RECORD cmd_Record = new SUBCMD_S_ROUND_RECORD();
		cmd_Record.m_vecRecordRoundPai = recordObj.m_vecRecordRoundPai;
		cmd_Record.m_vecRecordRoundScore = recordObj.m_vecRecordRoundScore;
		cmd_Record.m_vecRecordRoundScoreDetail = recordObj.m_vecRecordRoundScoreDetail;
		cmd_Record.m_vecRecordRoundXifen = recordObj.m_vecRecordRoundXifen;
		cmd_Record.m_vecPlayerChairId = recordObj.m_vecPlayerChairId;
		cmd_Record.m_names = recordObj.m_names;
		cmd_Record.m_nRound = m_byRound - 1;
		
		String sData_record = cmd_Record.getJsonData();
		
		// 大厅战绩统计
		JSONObject root = new JSONObject();
		String UIDList = "";
		String NameList = "";

		for(int i = 0; i < m_pIUserItem.length;i ++){
			if(m_pIUserItem.length - 1 != i){
				NameList += m_pIUserItem[i].szNickName + ",";
				UIDList += m_pIUserItem[i].dwUserID + ",";
			}else{
				UIDList += m_pIUserItem[i].dwUserID;
				NameList += m_pIUserItem[i].szNickName;
			}
		}
		
		String strRoundScore = IJsonExport.sprintf_BYTE(cmd_Record.m_vecRecordRoundScore, 5);
		String strRoundPai = IJsonExport.sprintf_BYTE(cmd_Record.m_vecRecordRoundPai, 45);
		String strRoundScoreDetail = IJsonExport.sprintf_BYTE(cmd_Record.m_vecRecordRoundScoreDetail, 15);
		String strPlayerChairId = IJsonExport.sprintf_BYTE(cmd_Record.m_vecPlayerChairId, 5);
		
		root.put("scoreList", strRoundScore);
		root.put("paiList", strRoundPai);
		root.put("ScoreDetailList", strRoundScoreDetail);
		root.put("PlayerChairIdList", strPlayerChairId);
		root.put("UIDList", UIDList);
		root.put("NameList", NameList);
		root.put("byRound", cmd_Record.m_nRound);
		
		Replay.AddReportData(root,GetType());
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData);
				SendData(m_pIUserItem[i].dwUserID, sData_record);
			}
		}
		
		OnEventGameEnd(Constants.INVALID_CHAIR, Constants.GER_NORMAL);
	}

	void KaiPai_PT() {
		int cbCardType[][] = new int[5][3];
		int vecReadyCard[][][] = new int[5][3][3];
		for (int i = 0; i < 5; i++) {
			vecReadyCard[i][0][0] = m_cbHandCardData[i][0];
			vecReadyCard[i][0][1] = m_cbHandCardData[i][1];
			vecReadyCard[i][0][2] = m_cbHandCardData[i][2];
			m_GameLogic.SortCardList(vecReadyCard[i][0], 3);
			cbCardType[i][0] = m_GameLogic.GetCardType(vecReadyCard[i][0], 3);

			vecReadyCard[i][1][0] = m_cbHandCardData[i][3];
			vecReadyCard[i][1][1] = m_cbHandCardData[i][4];
			vecReadyCard[i][1][2] = m_cbHandCardData[i][5];
			m_GameLogic.SortCardList(vecReadyCard[i][1], 3);
			cbCardType[i][1] = m_GameLogic.GetCardType(vecReadyCard[i][1], 3);

			vecReadyCard[i][2][0] = m_cbHandCardData[i][6];
			vecReadyCard[i][2][1] = m_cbHandCardData[i][7];
			vecReadyCard[i][2][2] = m_cbHandCardData[i][8];
	
			m_GameLogic.SortCardList(vecReadyCard[i][2], 3);
			cbCardType[i][2] = m_GameLogic.GetCardType(vecReadyCard[i][2], 3);
		}

		int roundTmpScore[] = new int[5];
		int roundSuanFen[][][] = new int[5][5][3];

		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		// 通比模式
		if (m_zhuangMode.equalsIgnoreCase("WUZHUANG")) {
			for (int i = 0; i < 5; i++) {
				int byNext = i;
				if (vecReadyCard[i][0][0] == 0) {
					continue;
				}
				for (int j = 0; j < 3; j++) {
					byNext = GetNextUser(byNext);
					if (vecReadyCard[byNext][0][0] == 0) {
						continue;
					}
					int curScore = 0;
					int prizeScore = 0;
					int cntWin = 0;
					int cntLose = 0;
					for (int k = 0; k < 3; k++) {
						int suanfen = 0;
						int ret = m_GameLogic.CompareCard(vecReadyCard[i][k], vecReadyCard[byNext][k], 3);

						if (k == 0) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 1) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 2) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						roundSuanFen[i][byNext][k] = suanfen;

					}

					roundTmpScore[i] += curScore;

				}
			}
		} else {
			if (true) {
				int byNext = m_byZhuang;

				for (int j = 0; j < 3; j++) {
					byNext = GetNextUser(byNext);
					if (vecReadyCard[byNext][0][0] == 0) {
						continue;
					}
					int curScore = 0;
					int prizeScore = 0;
					int cntWin = 0;
					int cntLose = 0;
					for (int k = 0; k < 3; k++) {
						int suanfen = 0;
						int ret = m_GameLogic.CompareCard(vecReadyCard[m_byZhuang][k], vecReadyCard[byNext][k], 3);

						if (k == 0) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 1) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						if (k == 2) {
							if (ret == 2) {
								cntWin++;

								curScore += 1;
								suanfen = 1;

							}
							if (ret == 1) {
								cntLose++;

								curScore += -1;
								suanfen = -1;

							}
							if (ret == 0) {
								curScore = 0;
							}
						}

						roundSuanFen[m_byZhuang][byNext][k] = suanfen;
						roundSuanFen[byNext][m_byZhuang][k] = 0 - suanfen;

					}
					roundTmpScore[m_byZhuang] += curScore;
					roundTmpScore[byNext] -= curScore;					
				}
			}

		}
		
	

		for (int i = 0; i < 5; i++) {
			m_vecRoundScore[i] = roundTmpScore[i];
		}
		SUBCMD_S_SuanFen cmd = new SUBCMD_S_SuanFen();

		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				for (int k = 0; k < 3; k++) {
//					cmd.nScore[i][j][k] = roundSuanFen[i][j][k];
				}
			}
		}

		String sData = cmd.getJsonData();
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData);
			}
		}

		OnEventGameEnd(Constants.INVALID_CHAIR, Constants.GER_NORMAL);
	}

	// 就绪事件
	protected boolean OnUserReady(int wChairID) {
		if (wChairID >= 5) {
			System.out.printf("Error wChairID (%d)\n", wChairID);
			return false;
		}
		m_wRobotSeconds[wChairID] = 0;
		// 状态判断
		if (m_bGameStarted)
			return false;

		m_pIUserItem[wChairID].cbUserStatus = US_READY;

		SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
		cmd.m_lUid = m_pIUserItem[wChairID].dwUserID;
		cmd.m_wUserChairID = (short) wChairID;// 玩家座位号

		String sData = cmd.getJsonData();
		SendData(Constants.INVALID_CHAIR, sData);

		return false;
	}

	void OnEventGameStart() {
		Init(Constants.INIT_NEWROUND);

		m_curStage = "STAGE_START";

		//通过创建来获得
		//m_kouWinSumJu = GoldMgr.getInstance().getKouWinJuShuInClub();
		SUBCMD_S_Start cmd = new SUBCMD_S_Start();
		cmd.byRound = m_byRound;
		cmd.byMaxRound = m_byMaxRound;
		for (int i = 0; i < 5; i++) {
			m_vecRoundScore[i] = 0;
			if (m_pIUserItem[i].dwUserID > 0 && (m_pIUserItem[i].cbUserStatus == Constants.US_READY
					|| m_pIUserItem[i].cbUserStatus == Constants.US_PLAY)) {
				m_pIUserItem[i].cbUserStatus = Constants.US_PLAY;
				cmd.vecIsReady[i] = 1;
			} else {
				// 旁观者的状态不能清空
				if(Constants.US_LOOKON != m_pIUserItem[i].cbUserStatus)
				{
					m_pIUserItem[i].cbUserStatus = Constants.US_NULL;
				}
				
				cmd.vecIsReady[i] = 0;
			}
		}

		String sData = cmd.getJsonData();
		SendData(Constants.INVALID_CHAIR, sData);

		if (m_zhuangMode.equalsIgnoreCase("WUZHUANG")) {
			FaPai();
		} else {
			needDealTime = 0;
			if (m_zhuangMode.equalsIgnoreCase("LUNZHUANG") || m_zhuangMode.equalsIgnoreCase("DINGZHUANG")) {
				needDealTime = 2;
			}
			if (m_zhuangMode.equalsIgnoreCase("QIANGZHUANG")) {
				needDealTime = 8;
			}
		}

		m_bTableStarted = true;
		m_bGameStarted = true;
	}
	public void DoDismissRoomByOverTime(){
		OnEventGameEnd(0, Constants.GAME_END_DISMISS);
	}
	void OnEventGameEnd(int bySeatID, int cbReason) {
		if(this.isZuJuType()){
			OnEventGameEndZuJu(bySeatID,cbReason);
		}else if(this.isGoldType()){
			OnEventGameEndGold(bySeatID,cbReason);
		}else if(this.isDwcType()){
			OnEventGameEndDwc(bySeatID,cbReason);
		}
	}
	void OnEventGameEndDwc(int bySeatID, int cbReason){
		m_byRound = m_byRound + 1;
		OnRoundInfoChange();
		m_curStage = "STAGE_JIESUAN";
		needDealTime = 10;

		m_bGameStarted = false;
		m_bPeiPaiReady = false;
		m_bLookOnDownTimeStart = true;

		ConcludeGame();
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}

		if (cbReason != Constants.GAME_END_DISMISS) {
			double shuishourate = getShuiShou(this.GetType());
			double shui = this.m_doubleDiFen * shuishourate;
			if(shui < 0.01)
				shui = 0.0;
			double[] realshui = new double[5];
			for(int i = 0;i < 5;i++){
				realshui[i] = 0;
			}
			//扣税，比鸡是通比，全部扣税
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				if(m_kouShuiMode.equals("no"))
					continue;
				realshui[i] = shui;
				SecShmUser user = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				//扣除税收先,在扣除UU都的时候，最多扣完,不会扣成负数(系统不补贴)
				m_pIUserItem[i].lUserRmb -= shui;
				user.getRmb1v1(0 - shui);
				if(m_pIUserItem[i].isAndroidUser == 0){
					UserRmbChange.handleRmbChangeKouShui(user, shui, UserRmbChange.reason29);
					this.addEatNormalScore(shui);
					handleRmbGiveBack(m_pIUserItem[i].dwUserID,shui);
					UserRedisInfo.addKouShuiFanXianForRent(m_pIUserItem[i].dwUserID,shui);
				}else{
					//机器人
					this.addEatRobotScore(shui);
				}
			}
			//乘上底分
			double vecDoubleScore[] = new double[5];
			for(int i = 0;i < 5;i++){
				vecDoubleScore[i] = m_vecRoundScore[i];
				vecDoubleScore[i] *= m_doubleDiFen;
			}
			//计算比率，系统不补贴
			double userCanWin = 0;
			double userCanLose = 0;
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				if(vecDoubleScore[i] > 0)
					userCanWin += vecDoubleScore[i];
				else if(vecDoubleScore[i] < 0)
					userCanLose += m_pIUserItem[i].lUserRmb;
			}
			if(userCanWin > userCanLose){
				//按照比率计算，系统不补贴
				double rate = userCanLose * 1.0 / userCanWin;
				
				for(int i = 0;i < 5;i++){
					vecDoubleScore[i] = vecDoubleScore[i] * rate;
				}
			}
			for(int i = 0;i < 5;i++){
				BigDecimal b = new BigDecimal(vecDoubleScore[i]);
				vecDoubleScore[i] = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 
			}
			//扣赢的
			if((this.inHall() || this.m_kouWinSumJu == 0) && m_dwcKouYing != 0 && m_dwcKouYingRate > 0){
				for(int i = 0;i < 5;i++){
					//不区别机器人还是真人
					if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
						continue;
					}
					//只扣赢家的，输家输的不变
					if(vecDoubleScore[i] > 0){
						double kou = vecDoubleScore[i] * m_dwcKouYingRate;
						BigDecimal b = new BigDecimal(kou);
						kou = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
						vecDoubleScore[i] -= kou;
						if(m_pIUserItem[i].isAndroidUser == 0){
							//只记录真人的
							UserRedisInfo.addKouYingFanXianForRent(m_pIUserItem[i].dwUserID,kou);
							double givesys = (1.0 - m_dwcKouYingFanliRate) * kou;
							double giveagent = m_dwcKouYingFanliRate * kou;
							BigDecimal b1 = new BigDecimal(givesys);
							givesys = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
							BigDecimal b2 = new BigDecimal(giveagent);
							giveagent = b2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
							UserRedisInfo.addKouYingFanXian(m_pIUserItem[i].dwUserID, giveagent);
							DwcRedisRecord.getInstance().addRmbGameInfo(UserRmbChange.reason30, givesys);
						}
					}
				}
			}

			boolean isYeJi = true;
			if(this.inUserClub() && this.m_kouWinSumJu > 0){
				for(int i = 0;i < 5;i++){
					m_doubleKouWinResultScore[i] += vecDoubleScore[i];
				}
				this.m_kouWinCurrJu++;
				if(this.m_kouWinSumJu == this.m_kouWinCurrJu){
					//扣除业绩
					for(int i = 0;i < 5;i++){
						if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
							continue;
						}
						//只扣赢家的，输家输的不变
						if(m_doubleKouWinResultScore[i] > 0){
							double kou = m_doubleKouWinResultScore[i] * m_dwcKouYingRate;
							BigDecimal b = new BigDecimal(kou);
							kou = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
							m_doubleVecResultScore[i] -= kou;

							SecShmUser safeuser = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
							m_pIUserItem[i].lUserRmb -= kou;
							safeuser.getRmb1v1(0 - kou);
							
							if(m_pIUserItem[i].isAndroidUser == 0){
								//只记录真人的
								UserRedisInfo.addKouYingFanXianForRent(m_pIUserItem[i].dwUserID,kou);
								double givesys = (1.0 - m_dwcKouYingFanliRate) * kou;
								double giveagent = m_dwcKouYingFanliRate * kou;
								BigDecimal b1 = new BigDecimal(givesys);
								givesys = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
								BigDecimal b2 = new BigDecimal(giveagent);
								giveagent = b2.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
								UserRedisInfo.addKouYingFanXian(m_pIUserItem[i].dwUserID, giveagent);
								DwcRedisRecord.getInstance().addRmbGameInfo(UserRmbChange.reason18, givesys);
							}
						}
					}
					this.m_kouWinCurrJu = 0;
					for(int i = 0;i < 5;i++){
						if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
							continue;
						}
						m_doubleKouWinResultScore[i] = 0.0;
					}
				}else{
					isYeJi = false;
				}
			}
			//记录
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				m_pIUserItem[i].lUserScore += vecDoubleScore[i];
				m_pIUserItem[i].lUserRmb += vecDoubleScore[i];
				m_doubleVecResultScore[i] += vecDoubleScore[i];
				SecShmUser xianUser = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				xianUser.getRmb1v1(vecDoubleScore[i]);				
				UserRmbChange.handleRmbChangeWinLose(xianUser,vecDoubleScore[i],UserRmbChange.reason30);
				if(m_pIUserItem[i].isAndroidUser == 2){
					//robot win & lost
					this.addRobotWinScore(vecDoubleScore[i]);
				}
			}//广播
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				if(vecDoubleScore[i] > GoldMgr.getInstance().getRmbBroadCastLimit()){
					//大于进行广播
					net.sf.json.JSONObject broad = new net.sf.json.JSONObject();
					broad.put("command","public_logic_broad_user");
					broad.put("value1", m_pIUserItem[i].szNickName);
					broad.put("value2", "安徽比鸡");
					broad.put("value3", vecDoubleScore[i]);
					broad.put("value4", "");
					AdminRedis.getInstance().pubAdminSubMsg(broad);
				}
			}
			//加入业绩
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				if(m_pIUserItem[i].isAndroidUser == 0 && isYeJi){
					//只处理机器人,正负都处理
					UserRedisInfo.addYeJiFanXian(m_pIUserItem[i].dwUserID,Math.abs(vecDoubleScore[i]),m_clubOwnerId);
				}
			}
			{
				SUBCMD_S_JieSuan cmd = new SUBCMD_S_JieSuan();
				cmd.byRound = m_byRound;
				cmd.lshui = realshui;

				for (int i = 0; i < 5; i++) {
					cmd.vecIsReady[i] = m_vecIsKaiPaiReady[i];
					Constants.CopyMemory(cmd.byPai[i], m_cbHandCardData[i], 9);
				}
				for (int i = 0; i < 5; i++) {
					cmd.nRoundScore[i] = vecDoubleScore[i];
					cmd.nUserScore[i] = m_pIUserItem[i].lUserRmb;
				}
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			}
		}

		if (m_byRound <= 1 && cbReason == Constants.GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			beforeDestroy(cloneServerUserPack(m_pIUserItem),null,m_doubleVecResultScore);
			QpGameMgr.getInstance().OnDismissRoom(this);

		} else if (m_byRound > m_byMaxRound || cbReason == Constants.GAME_END_DISMISS) {
			if(m_byRound > m_byMaxRound){
				for(int i = 0;i < 5;i++){
					if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
						continue;
					}
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if(user != null){
						user.getDwcJuShu(1);
					}
				}
			}

			SUBCMD_S_REPORT cmd = new SUBCMD_S_REPORT();
			cmd.cbReason = cbReason;
			cmd.whoDismiss = 0;

			for (int k = 0; k < 5; k++) {
				cmd.vecWinScore[k] = m_vecWinScore[k];
				cmd.vecLoseScore[k] = m_vecLoseScore[k];
				cmd.vecResultScore[k] = m_doubleVecResultScore[k];
				cmd.vecDetailGroupScore[k] = m_vecDetailGroupScore[k];
			}

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			m_bTableStarted = false;
			DelayRemove();

			beforeDestroy(cloneServerUserPack(m_pIUserItem),null,m_doubleVecResultScore);
			QpGameMgr.getInstance().OnDismissRoom(this);
		}
	}
	void OnEventGameEndGold(int bySeatID, int cbReason){
		m_byRound = m_byRound + 1;
		OnRoundInfoChange();
		m_curStage = "STAGE_JIESUAN";
		needDealTime = 10;

		m_bGameStarted = false;
		m_bPeiPaiReady = false;
		m_bLookOnDownTimeStart = true;

		ConcludeGame();
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}

		if (cbReason != Constants.GAME_END_DISMISS) {
			double shuishourate = getShuiShou(this.GetType());
			int shui = (int) (this.m_difen * shuishourate);
			double[] realshui = new double[5];
			for(int i = 0;i < 5;i++){
				realshui[i] = 0;
			}
			//扣税，比鸡是通比，全部扣税
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				realshui[i] = shui;
				SecShmUser user = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				//扣除税收先,在扣除UU都的时候，最多扣完,不会扣成负数(系统不补贴)
				m_pIUserItem[i].lUserCoin -= shui;
				user.getCoin(0 - shui);
				if(m_pIUserItem[i].isAndroidUser == 0){
					UserGoldChange.handleGoldChangeKouShui(user, shui, UserGoldChange.reason29);
					this.addEatNormalScore(shui);
					handleGoldGiveBack(m_pIUserItem[i].dwUserID,shui);
				}else{
					//机器人
					this.addEatRobotScore(shui);
				}
			}
			//乘上底分
			for(int i = 0;i < 5;i++){
				m_vecRoundScore[i] *= m_difen;
			}
			//计算比率，系统不补贴
			int userCanWin = 0;
			int userCanLose = 0;
			for(int i = 0;i < 5;i++){
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				if(m_vecRoundScore[i] > 0)
					userCanWin += m_vecRoundScore[i];
				else if(m_vecRoundScore[i] < 0)
					userCanLose += m_pIUserItem[i].lUserCoin;
			}
			if(userCanWin > userCanLose){
				//按照比率计算，系统不补贴
				double rate = userCanLose * 1.0 / userCanWin;
				
				for(int i = 0;i < 5;i++){
					m_vecRoundScore[i] = (int)(m_vecRoundScore[i] * rate);
				}
			}
			//记录
			for (int i = 0; i < 5; i++) {
				if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
					continue;
				}
				m_pIUserItem[i].lUserScore += m_vecRoundScore[i];
				m_pIUserItem[i].lUserCoin += m_vecRoundScore[i];
				m_vecResultScore[i] += m_vecRoundScore[i];
				SecShmUser xianUser = dthall.getInstance().getSecurityUserInfo(m_pIUserItem[i].dwUserID);
				xianUser.getCoin(m_vecRoundScore[i]);				
				UserGoldChange.handleGoldChangeWinLose(xianUser,m_vecRoundScore[i],UserGoldChange.reason30);
				if(m_pIUserItem[i].isAndroidUser == 2){
					//robot win & lost
					this.addRobotWinScore(m_vecRoundScore[i]);
				}
			}

			{
				SUBCMD_S_JieSuan cmd = new SUBCMD_S_JieSuan();
				cmd.byRound = m_byRound;
				cmd.lshui = realshui;
				for (int i = 0; i < 5; i++) {
					cmd.vecIsReady[i] = m_vecIsKaiPaiReady[i];
					Constants.CopyMemory(cmd.byPai[i], m_cbHandCardData[i], 9);
				}
				for (int i = 0; i < 5; i++) {
					cmd.nRoundScore[i] = m_vecRoundScore[i];
					cmd.nUserScore[i] = m_pIUserItem[i].lUserCoin;
				}
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			}
		}

		if (m_byRound <= 1 && cbReason == Constants.GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			beforeDestroy(cloneServerUserPack(m_pIUserItem),m_vecResultScore,null);
			QpGameMgr.getInstance().OnDismissRoom(this);

		} else if (m_byRound > m_byMaxRound || cbReason == Constants.GAME_END_DISMISS) {
			if(m_byRound > m_byMaxRound){
				for(int i = 0;i < 5;i++){
					if (m_pIUserItem[i].dwUserID <= 0 || Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus) {
						continue;
					}
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if(user != null){
						user.getGoldJuShu(1);
					}
				}
			}

			SUBCMD_S_REPORT cmd = new SUBCMD_S_REPORT();
			cmd.cbReason = cbReason;
			cmd.whoDismiss = 0;

			for (int k = 0; k < 5; k++) {
				cmd.vecWinScore[k] = m_vecWinScore[k];
				cmd.vecLoseScore[k] = m_vecLoseScore[k];
				cmd.vecResultScore[k] = m_vecResultScore[k];
				cmd.vecDetailGroupScore[k] = m_vecDetailGroupScore[k];
			}

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			m_bTableStarted = false;
			DelayRemove();

			beforeDestroy(cloneServerUserPack(m_pIUserItem),m_vecResultScore,null);
			QpGameMgr.getInstance().OnDismissRoom(this);
		}
	}
	void OnEventGameEndZuJu(int bySeatID, int cbReason){
		m_byRound = m_byRound + 1;
		OnRoundInfoChange();
		m_curStage = "STAGE_JIESUAN";
		needDealTime = 10;

		m_bGameStarted = false;
		m_bPeiPaiReady = false;
		m_bLookOnDownTimeStart = true;

		ConcludeGame();
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}

		if (cbReason != Constants.GAME_END_DISMISS) {
			for (int i = 0; i < 5; i++) {
				m_pIUserItem[i].lUserScore += m_vecRoundScore[i];
				m_vecResultScore[i] += m_vecRoundScore[i];
			}

			{
				SUBCMD_S_JieSuan cmd = new SUBCMD_S_JieSuan();
				cmd.byRound = m_byRound;
//				cmd.m_lLookOnDownTime = m_lLookOnLeaveDownTime;

				for (int i = 0; i < 5; i++) {
					cmd.vecIsReady[i] = m_vecIsKaiPaiReady[i];
					Constants.CopyMemory(cmd.byPai[i], m_cbHandCardData[i], 9);
				}
				for (int i = 0; i < 5; i++) {
					cmd.nRoundScore[i] = m_vecRoundScore[i];
					cmd.nUserScore[i] = m_pIUserItem[i].lUserScore;
				}
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

//				for (int i = 0; i < 5; i++) {// 清除牌局数据
//					m_vecRoundScore[i] = 0;
//				}
			}
		}

		if (m_byRound <= 1 && cbReason == Constants.GAME_END_DISMISS) {
			SUBCMD_S_DismissRoom cmd = new SUBCMD_S_DismissRoom();
			cmd.lUid = 0;
			cmd.cbResult = 0;
			cmd.m_wUserChairID = 0;
			cmd.m_dwTableID = m_dwTableID;

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);

		} else if (m_byRound > m_byMaxRound || cbReason == Constants.GAME_END_DISMISS) {
			if(m_byRound > m_byMaxRound){
				for(int i = 0;i < 5;i++){
					SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
					if(user != null){
						user.getZuJuJuShu(1);
					}
				}
			}
			//打印战绩
			System.out.println("比鸡战绩统计:roomid=" + this.GetID());
			for(int i = 0;i < m_vecRoundRecord.length;i++){
				BJRoundRecord round = m_vecRoundRecord[i];
				if(round != null){
					String str = JsonUtils.serialize(round);
					System.out.println("第" + (i + 1) + "局,内容=" + str);
				}
			}
			int cost[] = new int[5];
			for (int i = 0; i < 5; i++) {
				cost[i] = 0;
			}
			m_byRound = m_byRound<m_byMaxRound?m_byRound:m_byMaxRound;
			if (m_byRound > 2) {
				if (m_payWay == "FZPAY") {
//					int cost_gem = 0;
//					for (int i = 0; i < 5; i++) {
//						if (m_pIUserItem[i].dwUserID > 0) {
//							cost_gem += m_byCostGem;
//						}
//					}
//					if (m_Creator > 0) {
//						SHMUser user = dthall.getInstance().getUserInfo(m_Creator);
//						if (user != null) {
//							user.getGem(0 - cost_gem);
//						}
//					} else {
//						SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[0].dwUserID);
//						if (user != null) {
//							user.getGem(0 - cost_gem);
//						}
//						cost[0] = 0 - cost_gem;
//					}
					float rate = m_byRound * 1.0f / m_byMaxRound;
					rate *= m_byCostGem;
					Float frate = new Float(rate);
					int cost_gem = frate.intValue();

					if (m_Creator > 0) {
						SHMUser user = dthall.getInstance().getUserInfo(m_Creator);
						if (user != null && cost_gem <= 100) {
							user.getGem(0 - cost_gem);
						}
					} else {
						SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[0].dwUserID);
						if (user != null && cost_gem <= 100) {
							user.getGem(0 - cost_gem);
						}
						cost[0] = 0 - cost_gem;
					}
					
				}
				if (m_payWay == "AAPAY") {
					float rate = m_byRound * 1.0f / m_byMaxRound;
					rate *= m_byCostGem;
					Float frate = new Float(rate);
					int cost_gem = frate.intValue();

					if (m_Creator > 0) {
						SHMUser user = dthall.getInstance().getUserInfo(m_Creator);
						if (user != null && cost_gem <= 100) {
							user.getGem(0 - cost_gem);
						}
					} else {
						for (int i = 0; i < 5; i++) {
							if (m_pIUserItem[i].dwUserID > 0) {
								SHMUser user = dthall.getInstance().getUserInfo(m_pIUserItem[i].dwUserID);
								if (user != null && cost_gem <= 20) {
									user.getGem(0 - cost_gem);
									cost[i] = 0 - cost_gem;
								}
							}
						}
					}
//					for (int i = 0; i < 5; i++) {
//						int lFangZhu = m_pIUserItem[i].dwUserID;
//						if (lFangZhu > 0) {
//							SHMUser user = dthall.getInstance().getUserInfo(lFangZhu);
//							if (user != null) {
//								user.getGem(0 - m_byCostGem);
//								cost[i] = 0 - m_byCostGem;
//							}
//						}
//					}
				}
				if (m_payWay == "WINPAY") {
					int cost_gem = 0;
					int maxResultScore = 0;
					int winner = 0;
					int maxWinScore = 0;
					for (int k = 0; k < 5; k++) {
						if (m_vecResultScore[k] > 0) {
							maxWinScore += m_vecResultScore[k];
						}
					}
					for (int k = 0; k < 5; k++) {
						if (m_vecResultScore[k] > maxResultScore) {
							winner = k;
							maxResultScore = m_vecResultScore[k];
						}
						if (m_pIUserItem[k].dwUserID > 0) {
							cost_gem += m_byCostGem;
						}
					}
					if (cost_gem > 0) {
						for (int k = 0; k < 5; k++) {
							if (m_vecResultScore[k] > 0) {
								int lFangZhu = m_pIUserItem[k].dwUserID;
								if (lFangZhu > 0) {
									SHMUser user = dthall.getInstance().getUserInfo(lFangZhu);
									int temp_cost = m_vecResultScore[k] * cost_gem / maxWinScore;
									if (temp_cost <= 1)
										temp_cost = 1;
									user.getGem(0 - temp_cost);
									cost[k] = 0 - temp_cost;
								}
							}
						}
					}
				}
				if (m_payWay == "BIGWINPAY") {
					int cost_gem = 0;
					int maxResultScore = 0;
					int winner = 0;
					for (int k = 0; k < 5; k++) {
						if (m_vecResultScore[k] > maxResultScore) {
							winner = k;
							maxResultScore = m_vecResultScore[k];
						}
						if (m_pIUserItem[k].dwUserID > 0) {
							cost_gem += m_byCostGem;
						}
					}
					if (winner < 5) {
						int lFangZhu = m_pIUserItem[winner].dwUserID;
						if (lFangZhu > 0) {
							SHMUser user = dthall.getInstance().getUserInfo(lFangZhu);
							if (user != null && m_byCostGem < 50) {
								user.getGem(0 - cost_gem);
								cost[winner] = 0 - cost_gem;
							}
						}
					}
				}
				if (m_payWay == "CLUBPAY") {
					float rate = m_byRound * 1.0f / m_byMaxRound;
					rate *= m_byCostGem;
					Float frate = new Float(rate);
					int cost_gem = frate.intValue();
					DbInfoMgr.getInstance().costClubOwnerGem(this.m_clubOwnerId,cost_gem);
				}
			}
			SUBCMD_S_REPORT cmd = new SUBCMD_S_REPORT();
			cmd.cbReason = cbReason;
			cmd.whoDismiss = 0;

			for (int k = 0; k < 5; k++) {
				cmd.vecWinScore[k] = m_vecWinScore[k];
				cmd.vecLoseScore[k] = m_vecLoseScore[k];
				cmd.vecResultScore[k] = m_vecResultScore[k];
				cmd.vecDetailGroupScore[k] = m_vecDetailGroupScore[k];
			}

			SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

			log_counter++;
			log_counter = log_counter % 100;
			Long t = System.currentTimeMillis() / 1000;
			Long battle_id = (t % 1000000) * 100 + log_counter;

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String created = df.format(new Date());
			{

				JSONObject root = new JSONObject();

				root.put("command", "save_battle_log");
				root.put("battle_id", battle_id);

				root.put("room_type", GetType());
				root.put("hallwfdesc", m_strParam);

				root.put("room_id", m_dwTableID);
				root.put("creator", GetCreator());

				root.put("jushu", m_byMaxRound);
				root.put("limitscore", 0);

				if (m_payWay == "FZPAY") {
					root.put("payway", 0);
				} else if (m_payWay == "AAPAY") {
					root.put("payway", 1);
				} else {
					root.put("payway", 2);
				}
				root.put("created", created);
				root.put("uid0", (int) m_pIUserItem[0].dwUserID);
				root.put("nickname0", m_pIUserItem[0].szNickName);
				root.put("score0", m_vecResultScore[0]);
				root.put("cost0", cost[0]);
				root.put("uid1", (int) m_pIUserItem[1].dwUserID);
				root.put("nickname1", m_pIUserItem[1].szNickName);
				root.put("score1", m_vecResultScore[1]);
				root.put("cost1", cost[1]);
				root.put("uid2", (int) m_pIUserItem[2].dwUserID);
				root.put("nickname2", m_pIUserItem[2].szNickName);
				root.put("score2", m_vecResultScore[2]);
				root.put("cost2", cost[2]);
				root.put("uid3", (int) m_pIUserItem[3].dwUserID);
				root.put("nickname3", m_pIUserItem[3].szNickName);
				root.put("score3", m_vecResultScore[3]);
				root.put("cost3", cost[3]);
				
				root.put("uid4", (int) m_pIUserItem[4].dwUserID);
				root.put("nickname4", m_pIUserItem[4].szNickName);
				root.put("score4", m_vecResultScore[4]);
				root.put("cost4", cost[4]);
				
				
				if(m_clubId != 0){
					//club
					root.put("clubid", m_clubId);
					root.put("wfdesc", m_wanFaStr);
					if(m_payWay == "CLUBPAY"){
						root.put("payway", 10);
					}
					ClubRedis.getInstance().addClubBattleLog(m_clubId,root.toString());
				}
				GameDB.getInstance().push_task(null, root);
				Replay.save(m_dwTableID, new org.json.JSONObject(root.toString()));
			}

			m_bTableStarted = false;
			for (int i = 0; i < 5; i++) {// 清除牌局数据
				m_vecResultScore[i] = 0;
			}

			DelayRemove();

			QpGameMgr.getInstance().OnDismissRoom(this);

		}

	}

	protected boolean ConcludeGame() {
		// 设置状态

		RepositTable();

		for (int i = 0; i < 5; ++i) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if(m_pIUserItem[i].cbUserStatus != Constants.US_LOOKON)
				{
					m_pIUserItem[i].cbUserStatus = US_NULL;
				}			
			}
		}

		return true;
	}

	// 复位桌子
	public void RepositTable() {
		// 状态变量
		m_bGameStarted = false;

	}

	static int log_counter = 0;

	void OnDealQiangZhuang() {

		m_curStage = "STAGE_QIANGZHUANG";
		needDealTime = 3;

		List<Integer> vecQiangZhuang = new ArrayList<>();
		List<Integer> vecTmpUser = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				if (m_byQiangZhuang[i] > 0) {
					vecQiangZhuang.add(i);
				}
				vecTmpUser.add(i);
			}
		}
		int byQiangZhuang[] = new int[5];
		SUBCMD_S_DingZhuang cmd = new SUBCMD_S_DingZhuang();
		cmd.lUid = 0;
		cmd.wChairID = 0;
		cmd.wPower = 0;
		if (vecQiangZhuang.size() > 0) {
			int byZhuang = vecQiangZhuang.get(Constants.rand() % vecQiangZhuang.size());
			if (m_byQiangZhuang[byZhuang] == 0) {
				m_byQiangZhuang[byZhuang] = 1;
			}
			cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
			cmd.wChairID = byZhuang;
			cmd.wPower = m_byQiangZhuang[byZhuang];

			for (int i = 0; i < vecQiangZhuang.size(); i++) {
				byQiangZhuang[vecQiangZhuang.get(i)] = m_byQiangZhuang[vecQiangZhuang.get(i)];
			}
			m_byZhuang = byZhuang;
		} else {
			if (vecTmpUser.size() > 0) {
				int byZhuang = vecTmpUser.get(Constants.rand() % vecTmpUser.size());
				if (m_byQiangZhuang[byZhuang] == 0) {
					m_byQiangZhuang[byZhuang] = 1;
				}
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].dwUserID > 0) {
					} else {
						m_byQiangZhuang[i] = 0;
					}
				}
				cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
				cmd.wChairID = byZhuang;
				cmd.wPower = m_byQiangZhuang[byZhuang];

				for (int i = 0; i < vecTmpUser.size(); i++) {
					byQiangZhuang[vecTmpUser.get(i)] = m_byQiangZhuang[vecTmpUser.get(i)];
					if (byQiangZhuang[vecTmpUser.get(i)] < 0 || byQiangZhuang[vecTmpUser.get(i)] > 250) {
						byQiangZhuang[vecTmpUser.get(i)] = 1;
					}
				}
				m_byZhuang = byZhuang;
			} else {
				return;
			}

		}
		for (int i = 0; i < 5; i++) {
			cmd.byQiangZhuang[i] = byQiangZhuang[i];
		}

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

	}

	void QiangZhuang(int byZhuang,int delay_time) {
		m_curStage = "STAGE_QIANGZHUANG";
		needDealTime = delay_time;

		SUBCMD_S_DingZhuang cmd = new SUBCMD_S_DingZhuang();
		cmd.lUid = 0;
		cmd.wChairID = 0;
		cmd.wPower = 0;

		if (m_byQiangZhuang[byZhuang] == 0) {
			m_byQiangZhuang[byZhuang] = 1;
		}
		cmd.lUid = m_pIUserItem[byZhuang].dwUserID;
		cmd.wChairID = byZhuang;
		cmd.wPower = m_byQiangZhuang[byZhuang];

		m_byZhuang = byZhuang;

		cmd.byQiangZhuang[byZhuang] = 1;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	void OnDealAI(float dt) {
		m_DealTime = (int) (System.currentTimeMillis() / 1000);
		if (m_wFaPaiDelay > 0) {
			m_wFaPaiDelay--;
			return;
		}
		int cntUser = 0;
		for (int wChairID = 1; wChairID < 5; wChairID++) {
			if (m_pIUserItem[wChairID].dwUserID > 0) {
				cntUser++;
			}
		}
		if (m_curStage == "STAGE_START") {
			if (m_zhuangMode.equalsIgnoreCase("QIANGZHUANG")) {
				QiangZhuang(m_byZhuang,3);
			} else if (m_zhuangMode.equalsIgnoreCase("LUNZHUANG")) {
				int byNextUser = GetNextUser(m_byZhuang);
				QiangZhuang(byNextUser,1);
			} else if (m_zhuangMode.equalsIgnoreCase("DINGZHUANG")) {
				QiangZhuang(m_byZhuang,1);
			} else {

			}
		} else if (m_curStage == "STAGE_QIANGZHUANG") {
			FaPai();
		} else if (m_curStage == "STAGE_FAPAI") {

		}
	}
	
	// 踢出旁观者
	public void kickLookOnUser(){
		for (int seat = 0;seat < m_pIUserItem.length ;seat ++ ) {
			if (Constants.US_LOOKON == m_pIUserItem[seat].cbUserStatus) {
				// 是旁观者直接踢出房间
				int uid = m_pIUserItem[seat].dwUserID;
				QpGameMgr.getInstance().removeUserRoom(uid);
				OnUserStandUp(uid,true);
			}
		}
	}

	@Override
	public void OnDeal(int dt) {
		Long tCur = System.currentTimeMillis();

//		if (m_bLookOnDownTimeStart) {
//			if (0 == m_lLookOnLeaveDownTime) {
//				m_lLookOnLeaveDownTime = tCur;
//			}else{
//				if(tCur - m_lLookOnLeaveDownTime >= Constants.LOOKER_LEAVE_COUNTDOWNTIME * 1000){
//					// 倒计时结束旁观者踢出房间
//					kickLookOnUser();
//				}
//			}
//		}

		if (m_bTableStarted == true) {
			if (needDealTime > 0) {
				needDealTime--;
				return;
			}
			if (m_wFaPaiDelay > 0) {
				m_wFaPaiDelay--;
				return;
			}
			if (m_bGameStarted) {
				OnDealAI(dt);
			} else {
				// OnEventGameStart();
			}
		}

		if (m_bPeiPaiReady) {
			m_nPeiPaiCountDown += dt;

			if (m_nPeiPaiCountDown >= Constants.PEIPAI_COUNTDOWNTIME * 1000) {
				m_bPeiPaiReady = false;
//				OnAutoPeiPaiLogic();
			}
		}
	}
	
	// 时间到了自动配牌
	public void OnAutoPeiPaiLogic(){
		m_nPeiPaiCountDown = 0;
		m_bPeiPaiReady = false;

		// 给倒计时结束还没有配牌的玩家配默认牌(弃牌的排除)
		for (int seat = 0;seat < m_cbHandCardData.length ; seat++) {
			if (0 == m_cbHandCardData[seat][0] 
				&&  0 == m_vecQiPaiPlayer[seat]
				&& m_pIUserItem[seat].dwUserID > 0) {
				// 默认从左边开始每三个一组
				m_GameLogic.SortCardList(m_bTableCardArray[seat], m_bTableCardArray[seat].length);
				SUBCMD_S_KaiPaiReady cmd = new SUBCMD_S_KaiPaiReady();

				// 先进行分组排序
				int[][] tmpCardArray = new int[3][3];// 存放三道牌牌值
				for (int k = 0; k < 3; k ++) {
					for (int l = 0; l < 3; l ++) {
						tmpCardArray[k][l] = m_bTableCardArray[seat][k * 3 + l];
					}					
				}

				// 冒泡排序
				int[] tmpCard = new int[4];
				for (int j = 0; j < 3; j++) {
					for (int k = 0; k < 3-1-j; k++) {
						int ret = m_GameLogic.CompareCard(tmpCardArray[k], tmpCardArray[k + 1], 3);
						if (2 == ret){
							tmpCard = tmpCardArray[k];
							tmpCardArray[k] = tmpCardArray[k + 1];
							tmpCardArray[k + 1] = tmpCard;
						}
					}
				}

				int idx = 0;
				for (int m = 0; m < 3; m ++) {
					for (int n = 0; n < 3; n ++) {
						m_cbHandCardData[seat][idx] = tmpCardArray[m][n];
						cmd.byPai[idx] = tmpCardArray[m][n];
						idx ++;
					}
				}

				m_vecIsKaiPaiReady[seat] = 1;
				cmd.byRound = m_byRound;
				cmd.wChairID = seat;
				for (int k = 0; k < 5; k++) {
					cmd.vecIsKaiPaiReady[k] = m_vecIsKaiPaiReady[k];
				}

				String sData = cmd.getJsonData();
				for (int chair = 0; chair < 5; chair++) {
					if (m_pIUserItem[chair].dwUserID > 0) {
						SendData(m_pIUserItem[chair].dwUserID, sData);
					}
				}
			}
		}

		// 开始比牌
		int cntKaiPaiReady = 0; // 准备人数
		int giveUpCnt = 0; // 弃牌人数
		int cntUser = 0;

		for (int i = 0; i < 5; i++) {
			if(1 == m_vecIsKaiPaiReady[i])
			{
				cntKaiPaiReady ++;
			}
			
			if(m_vecQiPaiPlayer[i] > 0)
			{
				giveUpCnt ++;
			}

			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser ++;
			}
		}

		if (m_bGameStarted) {
			if ((cntKaiPaiReady + giveUpCnt) == cntUser) {
				if (m_guize.equalsIgnoreCase("GUIZE_PT")) {
					KaiPai_PT();
				} else {
					KaiPai_HBL();
				}
				m_bGameStarted = false;
			}
		}
	}

	static int randRobotIndex = 999;

	void AddRobot() {
		randRobotIndex++;
		int robot_uid = randRobotIndex % 9999 + 200000;

		Vector<Integer> vecEmpty = new Vector<>();
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID == 0) {
				vecEmpty.add(i);

			}
		}
		if (vecEmpty.size() > 0) {
			int rand = Constants.rand(0, 10000);
			int wChairID = vecEmpty.get(Constants.rand() % vecEmpty.size());

			int vecMaleFace[] = { 1, 3, 5, 7, 9 };
			int vecFemaleFace[] = { 2, 4, 6, 8, 10 };
			m_pIUserItem[wChairID].dwUserID = robot_uid;
			m_pIUserItem[wChairID].lUserScore = 1000;
			m_pIUserItem[wChairID].wChairID = wChairID;
			m_pIUserItem[wChairID].cbGender = rand % 2;
			if (m_pIUserItem[wChairID].cbGender == 1) {
				m_pIUserItem[wChairID].dwFaceID = vecMaleFace[rand % 5];
			} else {
				m_pIUserItem[wChairID].dwFaceID = vecFemaleFace[rand % 5];
			}
			m_pIUserItem[wChairID].cbUserStatus = Constants.US_NULL;
			m_pIUserItem[wChairID].isAndroidUser = 1;

			m_pIUserItem[wChairID].szNickName = "USER[" + wChairID + "]";

			{
				SUBCMD_S_Sit cmd = new SUBCMD_S_Sit();
				// memset(&cmd, 0, sizeof(cmd));

				cmd.m_lUid = robot_uid;
				cmd.m_wUserChairID = m_pIUserItem[wChairID].wChairID;
				cmd.m_lUserScore = m_pIUserItem[wChairID].lUserScore;
				cmd.m_iAvatar = m_pIUserItem[wChairID].dwFaceID;
				cmd.szNickName = m_pIUserItem[wChairID].szNickName;
				cmd.szIP = "";
				cmd.m_nState = m_pIUserItem[wChairID].cbUserStatus;
				
				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			}
			{
				m_pIUserItem[wChairID].cbUserStatus = Constants.US_READY;

				SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
				// memset(&cmd, 0, sizeof(cmd));

				cmd.m_lUid = robot_uid;
				cmd.m_wUserChairID = m_pIUserItem[wChairID].wChairID;

				SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
			}
			m_wRobotSeconds[wChairID] = 0;
		}
	}

	void Handle_onJoin(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbOnline = Constants.US_NULL;
		}

		SUBCMD_S_Join cmd = new SUBCMD_S_Join();
		// memset(&cmd, 0, sizeof(cmd));

		String sData = cmd.getJsonData();
		SendData(_Uid, sData);
	}

	void Handle_onReady(int _Uid, String vecArg[]) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
		}
		int cntReady = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].cbUserStatus == Constants.US_READY) {
				cntReady++;
			}
		}
		
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntUser++;
			}
		}
		
		
		SUBCMD_S_Ready cmd = new SUBCMD_S_Ready();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = bySeatID;
		cmd.m_dwTableID = m_dwTableID;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
		if (cntReady == cntUser) {
			OnEventGameStart();
		} else {

		}
	}

	void Handle_onRobot(int _Uid, String vecArg[]) {
		if (m_bGameStarted == true) {
			return;
		}
		int bySeatID = FindSeat(_Uid);
		if (bySeatID < 5) {
			m_pIUserItem[bySeatID].cbUserStatus = Constants.US_READY;
		}
		int cntPlayer = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				cntPlayer++;
			}
		}
		if (cntPlayer < 5) {
			for (int i = 0; i < 5 - cntPlayer; i++) {
				AddRobot();
			}
		}
	}

	// 表情
	void Handle_onBiaoQing(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Constants.atoi(vecArg[1]);

		SUBCMD_S_BiaoQing cmd = new SUBCMD_S_BiaoQing();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iBiaoQingIndex = index;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}
	void Handle_onText(int _Uid, String[] vecArg) {

		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		String strContent = vecArg[1];
		String iter = dthall.getInstance().m_mapAvatar.get(_Uid);				
		
		String szAvatarURL = "http://lamj.qilehuyu.cn/liuan-80.png";

		if(!StringUtils.isEmpty(iter)){
			m_vecInputWenZi+=iter+";"+_Uid+";"+strContent+",";
		}
		else{
			m_vecInputWenZi+=szAvatarURL+";"+_Uid+";"+strContent+",";
		}
		strContent = string_replace(strContent, "|", ".");
		strContent = string_replace(strContent, "#", ".");
		strContent = string_replace(strContent, ",", ".");
		strContent = string_replace(strContent, "\\", ".");
		strContent = string_replace(strContent, "'", ".");
		strContent = string_replace(strContent, "\"", ".");

		SUBCMD_S_Text cmd = new SUBCMD_S_Text();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.szContent = strContent;
		cmd.InputWenZi=m_vecInputWenZi;
		SendData(cmd.getJsonData());
	}
	private String string_replace(String strContent, String string, String string2) {
		return strContent.replace(string, string2);
	}
	// 璇煶
	void Handle_onYuYin(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		int index = Constants.atoi(vecArg[1]);

		SUBCMD_S_YuYin cmd = new SUBCMD_S_YuYin();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.m_iYuYinIndex = index;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 送礼
	void Handle_onSongLi(int _Uid, String vecArg[]) {
	}

	// 聊天
	void Handle_onChat(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 2)
			return;
		String strContent = vecArg[1];

		SUBCMD_S_Chat cmd = new SUBCMD_S_Chat();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.szContent = strContent;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 语音
	void Handle_onVoice(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, wChairID);
			return;
		}
		if (vecArg.length < 3)
			return;
		int seconds = Constants.atoi(vecArg[1]);
		String strVoiceURL = vecArg[2];

		SUBCMD_S_Voice cmd = new SUBCMD_S_Voice();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;
		cmd.szVoiceURL = strVoiceURL;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}

	// 语音
	void Handle_onTryDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;
		String attachInfo = vecArg[1];
		if(attachInfo.equals("fzquit")){
			//房主请求直接退出
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			DelayRemove();
			QpGameMgr.getInstance().OnDismissRoom(this);
			return;
		}
		int cntUser = 0;
		int cntOnline = 0;
		int cntOffline = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				if (m_pIUserItem[i].cbOnline == Constants.US_OFFLINE) {
					cntOffline++;
				} else {
					cntOnline++;
				}
			}
		}
		if (cntUser > 1) {
			if (m_dismissTimer == 0) {
				m_dismissTimer = Constants.time(null);
			}
			m_byAgreeDismiss[bySeatID] = 1;
			
			int cntAgree = 0;

			for (int i = 0; i < 5; i++) {
				if (m_byAgreeDismiss[i] > 0 || (m_pIUserItem[i].dwUserID > 0 && Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus)) {
					cntAgree++;
				}
			}
			
			if (cntAgree >= 2 && cntAgree >= cntUser - 1) {
				OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			}else{
				SUBCMD_S_TryDismissRoom cmd = new SUBCMD_S_TryDismissRoom();
				cmd.lUid = _Uid;
				cmd.dwTableID = m_dwTableID;
				cmd.szNickName = m_pIUserItem[bySeatID].szNickName;
				
				for(int i = 0; i < 5; i ++)
				{	// 旁观者不参与解散房间
					if(m_pIUserItem[i].dwUserID > 0 && Constants.US_LOOKON != m_pIUserItem[i].cbUserStatus)
					{
						SendData(m_pIUserItem[i].dwUserID, cmd.getJsonData());
					}
				}
				
			
				if (cntOnline == 1) {
					OnEventGameEnd(0, Constants.GAME_END_DISMISS);

					m_dismissTimer = 0;
				}
			}
			
		} else {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
		}

	}

	// 语音
	void Handle_onAgreeDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;
		m_byAgreeDismiss[bySeatID] = 1;

		SUBCMD_S_AgreeDismissRoom cmd = new SUBCMD_S_AgreeDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

		if (bySeatID == 0 && m_bTableStarted == false) {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
			return;
		}
		int cntAgree = 0;

		for (int i = 0; i < 5; i++) {
			if (m_byAgreeDismiss[i] > 0 || (m_pIUserItem[i].dwUserID > 0 && Constants.US_LOOKON == m_pIUserItem[i].cbUserStatus)) {
				cntAgree++;
			}
		}
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].isAndroidUser == 0) {
				cntUser++;
			}
		}
		System.out.println("roomid=" + this.m_dwTableID + " cntAgree=" + cntAgree + " cntUser=" + cntUser);
		if (cntAgree >= 2 && cntAgree >= cntUser - 1) {
			OnEventGameEnd(0, Constants.GAME_END_DISMISS);
		}
	}
	//发送普通语音消息
	void Handle_onVoiceMsg(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		float seconds = Float.parseFloat(vecArg[1]);

		SUBCMD_S_VoiceMsg cmd = new SUBCMD_S_VoiceMsg();
		cmd.m_lUid = _Uid;
		cmd.m_wUserChairID = wChairID;
		cmd.seconds = seconds;

		cmd.data = vecArg[2];
		cmd.m_maxClip = Integer.parseInt(vecArg[3]);
		cmd.m_currClip = Integer.parseInt(vecArg[4]);
		SendData(cmd.getJsonData());
	}

	// 语音
	void Handle_onRefuseDismissRoom(int _Uid, String vecArg[]) {
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		if (vecArg.length < 2)
			return;

		m_dismissTimer = 0;
		m_byAgreeDismiss[0] = 0;
		m_byAgreeDismiss[1] = 0;
		m_byAgreeDismiss[2] = 0;
		m_byAgreeDismiss[3] = 0;
		m_byAgreeDismiss[4] = 0;

		SUBCMD_S_RefuseDismissRoom cmd = new SUBCMD_S_RefuseDismissRoom();
		cmd.lUid = _Uid;
		cmd.dwTableID = m_dwTableID;
		cmd.szNickName = m_pIUserItem[bySeatID].szNickName;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}
	void Handle_InBack(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == INVALID_CHAIR) {
			return;
		}
				
		SUBCMD_S_InBack cmd = new SUBCMD_S_InBack();
		cmd.byActionUser = wChairID;
		SendData(cmd.getJsonData());
	}
	int INVALID_CHAIR = Constants.INVALID_CHAIR;
	void Handle_InFore(int _Uid, String vecArg[]) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == INVALID_CHAIR) {
			return;
		}
		
		SUBCMD_S_InFore cmd = new SUBCMD_S_InFore();
		cmd.byActionUser = wChairID;
		SendData(cmd.getJsonData());
	}
	void Handle_onStart(int _Uid, String vecArg[]) {
		if (vecArg.length < 2)
			return;

		int bySeatID = FindSeat(_Uid);
		if (m_bTableStarted == false && bySeatID == 0 && GetPlayerCount() > 1) {
			OnEventGameStart();
		}
	}

	void Handle_onQiangZhuang(int _Uid, String vecArg[]) {
		if (vecArg.length < 3)
			return;

		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}

		m_byQiangZhuang[bySeatID] = 1;

		int power = 1;

		SUBCMD_S_QiangZhuang cmd = new SUBCMD_S_QiangZhuang();
		cmd.lUid = _Uid;
		cmd.wChairID = bySeatID;
		cmd.wPower = power;

		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());

		int curNum = 0;
		int XuanZhuangN = 0;
		for (int i = 0; i < 5; i++) {
			if (m_byQiangZhuang[i] == 1 || m_byQiangZhuang[i] == -1) {
				XuanZhuangN++;
			}
			if (m_pIUserItem[i].dwUserID > 0) {
				curNum++;
			}
		}
		if (XuanZhuangN >= curNum && m_curStage == "STAGE_START") {
			OnDealQiangZhuang();
		}
	}

	void Handle_onBuQiangZhuang(int _Uid, String vecArg[]) {
		if (vecArg.length < 3)
			return;
		int bySeatID = FindSeat(_Uid);
		if (bySeatID == Constants.INVALID_CHAIR) {
			// printf("Handle_onBiaoQing Error _Uid(%d)(%d)\n", _Uid, bySeatID);
			return;
		}
		m_byQiangZhuang[bySeatID] = -1;

		int curNum = 0;
		int XuanZhuangN = 0;
		for (int i = 0; i < 5; i++) {
			if (m_byQiangZhuang[i] == 1 || m_byQiangZhuang[i] == -1) {
				XuanZhuangN++;
			}
			if (m_pIUserItem[i].dwUserID > 0) {
				curNum++;
			}
		}
		if (XuanZhuangN >= curNum && m_curStage == "STAGE_START") {
			needDealTime = 8;
			OnDealQiangZhuang();
		}

	}

	public int FindSeat(int _Uid) {
		for (int bySeatID = 0; bySeatID < 5; bySeatID++) {
			if (m_pIUserItem[bySeatID].dwUserID == _Uid) {
				return bySeatID;
			}
		}
		return Constants.INVALID_CHAIR;
	}
	public int m_DealTime = 0;

	public int getDealTime() {
		return m_DealTime;
	}

	public boolean m_bRemove = false;

	public void DelayRemove() {
		m_bRemove = true;
	}

	public void ResetKillFlag() {
		m_bRemove = false;
	}

	public boolean ShouldBeRemoved() {
		return m_bRemove;
	}

	public void Handle_onLock(Integer lUid, String[] vecArg) {

		if (vecArg.length < 2)
			return;
		if (m_bTableStarted && m_pIUserItem[0].dwUserID == lUid) {// 已经开始 并且是房主
			int index = Constants.atoi(vecArg[1]);
			m_locked = index;
		}
	}
	
	public void Handle_onKick(Integer lUid, String[] vecArg) {
		if (vecArg.length < 2)
			return;
		if (m_bTableStarted && m_pIUserItem[0].dwUserID == lUid) {// 已经开始 并且不是房主
			int kick_uid  = Constants.atoi(vecArg[1]);
			int seat = FindSeat(kick_uid);
			if (Constants.US_LOOKON == m_pIUserItem[seat].cbUserStatus) {
				
				SUBCMD_S_Kick cmd = new SUBCMD_S_Kick();
				cmd.m_lUid = kick_uid;
				
				String sData = cmd.getJsonData();
				SendData(Constants.INVALID_CHAIR, sData);
				
				OnUserStandUp(kick_uid,true);
				
				// 是旁观者直接踢出房间
				QpGameMgr.getInstance().OnRemoveUserRoom(kick_uid);
				
				
				int cntReady = 0;
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].cbUserStatus == Constants.US_READY) {
						cntReady++;
					}
				}
				
				int cntUser = 0;
				for (int i = 0; i < 5; i++) {
					if (m_pIUserItem[i].dwUserID > 0) {
						cntUser++;
					}
				}
				
				if (cntReady == cntUser) {
					OnEventGameStart();
				}
			}
		}
		
		
	}
	
	private void SendData(String jsonData) {
		SendData(Constants.INVALID_CHAIR, jsonData);
	}
	
	//弃牌
	public void Handle_giveUp(Integer lUid, String[] vecArg){
		int wChairID = FindSeat(lUid);
//		m_vecIsKaiPaiReady[wChairID] = 1;
		m_vecIsGiveUp[wChairID] = 1;
		
		// 已经弃牌无须再次处理
		if(m_vecQiPaiPlayer[wChairID] > 0){
			return;
		}
		
		m_nGiveUpOrder ++;
		m_vecQiPaiPlayer[wChairID] = m_nGiveUpOrder;

		SUBCMD_S_GiveUp cmd = new SUBCMD_S_GiveUp();
		cmd.byRound = m_byRound;
		int cntKaiPaiReady = 0; // 准备人数
		int giveUpCnt = 0; // 弃牌人数
		for (int i = 0; i < 5; i++) {
//			cmd.vecIsKaiPaiReady[i] = m_vecIsKaiPaiReady[i];
			cmd.m_vecIsGiveUp[i] = m_vecIsGiveUp[i];
			if(1 == m_vecIsKaiPaiReady[i])
			{
				cntKaiPaiReady ++;
			}
			
			if(m_vecQiPaiPlayer[i] > 0)
			{
				giveUpCnt ++;
			}
		}
		
		String sData = cmd.getJsonData();
		SendData(Constants.INVALID_CHAIR, sData);
		
		// 判断是否可以开始比牌
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == 2) {
				cntUser ++;
			}
		}
		
		if (m_bGameStarted) {
			if ((cntKaiPaiReady + giveUpCnt) == cntUser) {
				if (m_guize.equalsIgnoreCase("GUIZE_PT")) {
					KaiPai_PT();
				} else {
					KaiPai_HBL();
				}
				m_bGameStarted = false;
			}
		}
		return;
	}
	void Handle_onExpr(int _Uid, String[] vecArg) {
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		int uid1 = Integer.parseInt(vecArg[1]);
		int uid2 = Integer.parseInt(vecArg[2]);
		int index = Integer.parseInt(vecArg[3]);
		
		SUBCMD_S_ExprMsg cmd = new SUBCMD_S_ExprMsg();
		cmd.m_uid1 = uid1;
		cmd.m_uid2 = uid2;
		cmd.m_index = index;
		
		SendData(Constants.INVALID_CHAIR, cmd.getJsonData());
	}
	void Handle_GameIap(int _Uid,String vecArg[]){
		int wChairID = FindSeat(_Uid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		SHMUser user = dthall.getInstance().getUserInfo(_Uid);
		if(user == null){
			return;
		}
		SUBCMD_S_GameIap cmd = new SUBCMD_S_GameIap();
		cmd.uid = _Uid;
		cmd.seat = wChairID;
		if(this.isDwcType()){
			cmd.coin = user.getRmb1v1Value();
			m_pIUserItem[wChairID].lUserRmb = cmd.coin;
		}else{
			cmd.coin = user.getCoin(0);
			m_pIUserItem[wChairID].lUserCoin = (int) cmd.coin;
		}
		SendData(Constants.INVALID_CHAIR,cmd.getJsonData());
	}
	public void Handle_QuickMatch(int _Uid,String vecArg[]){
		
	}
	public void Handle_onKaiPaiReady(Integer lUid, String[] vecArg) {

		if (vecArg.length < 2)
			return;
		int wChairID = FindSeat(lUid);
		if (wChairID == Constants.INVALID_CHAIR) {
			return;
		}
		String strTanPai = vecArg[1];
		int dwHandCard[] = new int[9];

		String vecArgTanPai[] = strTanPai.split("\\,");
		if (vecArgTanPai.length != 9) {
			return;
		}

		try {
			for (int i = 0; i < 9; i++) {
				dwHandCard[i] = Integer.parseInt(vecArgTanPai[i]);
			}
			for (int i = 0; i < 9; i++) {
				int cntCard = 0;
				for (int j = 0; j < 9; j++) {
					if (m_bTableCardArray[wChairID][j] == dwHandCard[i]) {
						cntCard++;
					}
				}
				if (cntCard != 1) {
					SUBCMD_S_Error cmd = new SUBCMD_S_Error();
					cmd.byRound = m_byRound;
					cmd.bySeatID = wChairID;
					cmd.errCode = 0;
					cmd.errMsg = "系统认定您配牌出错，请仔细检查您的牌";
					System.out.println("系统认定您配牌出错，请仔细检查您的牌");

					String sData = cmd.getJsonData();
					SendData(m_pIUserItem[wChairID].dwUserID, sData);

					return;
				}
			}
			int card0[] = new int[3];
			int card1[] = new int[3];
			int card2[] = new int[3];

			card0[0] = dwHandCard[0];
			card0[1] = dwHandCard[1];
			card0[2] = dwHandCard[2];
			
			card1[0] = dwHandCard[0 + 3];
			card1[1] = dwHandCard[1 + 3];
			card1[2] = dwHandCard[2 + 3];
			
			card2[0] = dwHandCard[0 + 6];
			card2[1] = dwHandCard[1 + 6];
			card2[2] = dwHandCard[2 + 6];
			m_GameLogic.SortCardList(card0, 3);
			m_GameLogic.SortCardList(card1, 3);
			m_GameLogic.SortCardList(card2, 3);
			int ret1 = m_GameLogic.CompareCard(card0, card1, 3);
			int ret2 = m_GameLogic.CompareCard(card1, card2, 3);

			if (ret1 == 2 || ret2 == 2) {
				SUBCMD_S_Error cmd = new SUBCMD_S_Error();
				cmd.byRound = m_byRound;
				cmd.bySeatID = wChairID;
				cmd.errCode = 0;
				cmd.errMsg = "第二道必须大于第一道小于第三道";
				
				System.out.println(Arrays.toString(card0));
				System.out.println(Arrays.toString(card1));
				System.out.println(Arrays.toString(card2));
				System.out.println("*** Error: The first must be smaller than the second !");
				String sData = cmd.getJsonData();
				SendData(m_pIUserItem[wChairID].dwUserID, sData);

				return;
			}
			for (int i = 0; i < 9; i++) {
				m_cbHandCardData[wChairID][i] = dwHandCard[i];
			}
			// System.out.println(IJsonExport.sprintf_BYTE(m_cbHandCardData[wChairID],
			// 13));
		} catch (Exception e) {
			e.printStackTrace();
		}

		m_vecIsKaiPaiReady[wChairID] = 1;
		int cntTanPai = 0;
		for (int i = 0; i < 5; i++) {
			if (m_cbHandCardData[i][0] > 0) {
				cntTanPai++;
			}
		}
		SUBCMD_S_KaiPaiReady cmd = new SUBCMD_S_KaiPaiReady();
		cmd.byRound = m_byRound;
		cmd.byPai = m_cbHandCardData[wChairID];
		cmd.wChairID = wChairID;
		int cntKaiPaiReady = 0; // 准备人数
		int giveUpCnt = 0; // 弃牌人数
		for (int i = 0; i < 5; i++) {
			cmd.vecIsKaiPaiReady[i] = m_vecIsKaiPaiReady[i];
			
			if(1 == m_vecIsKaiPaiReady[i])
			{
				cntKaiPaiReady ++;
			}
			
			if(m_vecQiPaiPlayer[i] > 0)
			{
				giveUpCnt ++;
			}
		}

		String sData = cmd.getJsonData();
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0) {
				SendData(m_pIUserItem[i].dwUserID, sData);
			}
		}
		
		int cntUser = 0;
		for (int i = 0; i < 5; i++) {
			if (m_pIUserItem[i].dwUserID > 0 && m_pIUserItem[i].cbUserStatus == 2) {
				cntUser ++;
			}
		}
		
		if (m_bGameStarted) {
			System.out.println("roomid=" + this.m_dwTableID);
			System.out.println("cntKaiPaiReady + giveUpCnt=" + cntKaiPaiReady + giveUpCnt);
			System.out.println("cntUser=" + cntUser);
			if ((cntKaiPaiReady + giveUpCnt) == cntUser) {
				if (m_guize.equalsIgnoreCase("GUIZE_PT")) {
					KaiPai_PT();
				} else {
					KaiPai_HBL();
				}
				m_bGameStarted = false;
			}
		}
	}

	@Override
	public String getRoomInfo() {
		// TODO 自动生成的方法存根
				//m_lStartTime
				//m_pIUserItem
				// m_bTableStarted;
				// m_byRound;
				// m_byMaxRound;
				//m_dwTableID
				String roomInfo = String.format("%s/%s/%s/%s/%s", 
						m_pIUserItem[0].GetUserInfo(), //#
						m_pIUserItem[1].GetUserInfo(), 
						m_pIUserItem[2].GetUserInfo(), 
						m_pIUserItem[3].GetUserInfo(),
						m_pIUserItem[4].GetUserInfo()
						);
				
				String[] vecParam = m_strParam.split(";");
				String vecParamStr = "";
				for (int i = 0; i < vecParam.length; i++) {
					String param = vecParam[i];

					if (param.equalsIgnoreCase("8JU")) {
						vecParamStr += "8局 ";
					}
					if (param.equalsIgnoreCase("16JU")) {
						vecParamStr += "16局 ";
					}
					if (param.equalsIgnoreCase("WUZHUANG")) {
						
						vecParamStr += "通比模式 ";
					}
					if (param.equalsIgnoreCase("GUIZE_HBL")) {
					
						vecParamStr += "有奖金模式";
					}
					if (param.equalsIgnoreCase("GUIZE_PT")) {
					
						vecParamStr += "无普通模式 ";
					}
					
				}
				
				int start = 0;
				if(m_bTableStarted){
					start = 1;
				}
				String descInfo = String.format("比鸡 %s",vecParamStr);
				roomInfo = String.format("%s|%d|%d|%d|%s|%d|%d|%d|%s|%s", roomInfo,m_dwTableID,m_lStartTime,start,descInfo,m_byMaxUser,m_byRound,m_byMaxRound,"比鸡","BJZ");
				
				return roomInfo;
	}

	@Override
	public String getRoomDesc() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getPlayerCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCostGem() {
		// TODO Auto-generated method stub
		return m_byCostGem;
	}

	@Override
	public int getCellScore() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getMinScore() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getStatus() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public String getPayway() {
		// TODO Auto-generated method stub
		return m_payWay;
	}

	@Override
	public String GetRoundInfo() {
		return "" + m_byRound + "/" +  m_byMaxRound;
	}
}
